Shell脚本安装成服务加入系统启动-service

Shell脚本自动安装为系统服务
这篇博客介绍如何将Shell脚本配置为系统服务,适用于多种操作系统,并涵盖了systemd教程及常见问题解决。
#!/bin/bash
# 向一个临时文件中写入配置的内容,两种形式service和systemctl
sudo cat >/tmp/mysql-exporter-systemctl<<EOF
[Unit]
Description=Mysql Exporter
After=network.target
[Service]
ExecStart= ...
[Install]
WantedBy=default.target
EOF

sudo cat >/tmp/mysql-exporter-service<<EOF
#!/bin/bash
case "\$1" in
start)
   ...
   echo \$!>/var/run/[servicename].pid
   ;;
stop)
   sudo kill \`cat /var/run/[servicename].pid\`
   sudo rm -f /var/run/[servicename].pid
   ;;
restart)
   \$0 stop
   \$0 start
   ;;
status)
   if [ -e /var/run/[servicename].pid ]; then
      echo [servicename] is running, pid=\`cat /var/run/[servicename].pid\`
   else
      echo [servicename] is NOT running
      exit 1
   fi
   ;;
*)
   echo "Usage: \$0 {start|stop|status|restart}"
esac

exit 0
EOF

function service_conf(){
  # 判断使用那种形式的配置内容
  if [ -x /usr/bin/systemctl ]; then
    sudo mv /tmp/mysql-exporter-systemctl /usr/lib/systemd/system/[servicename].service
    sudo chmod 754 /usr/lib/systemd/system/[servicename].service
    sudo systemctl daemon-reload
    sudo systemctl enable [servicename].service
  elif [ -x /bin/systemctl ]; then
    sudo mv /tmp/mysql-exporter-systemctl /lib/systemd/system/[servicename].service
    sudo chmod 754 /lib/systemd/system/[servicename].service
    sudo systemctl daemon-reload
    sudo systemctl enable [servicename].service
  else
    sudo mv /tmp/mysql-exporter-service /etc/init.d/[servicename]
    sudo chmod 755 /etc/init.d/[servicename]
  fi
}

service_conf

这个脚本使用于多个操作系统

Systemd 默认从目录/etc/systemd/system/读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录。

systemctl enable命令用于在上面两个目录之间,建立符号链接关系。


$ sudo systemctl enable clamd@scan.service
# 等同于
$ sudo ln -s '/usr/lib/systemd/system/clamd@scan.service' '/etc/systemd/system/multi-user.target.wants/clamd@scan.service'
如果配置文件里面设置了开机启动,systemctl enable命令相当于激活开机启动。

与之对应的,systemctl disable命令用于在两个目录之间,撤销符号链接关系,相当于撤销开机启动。

systemd的教程以及可能遇到的问题

在Linux系统中,将shell脚本加入开机自启动有多种方法: - **方法一:传统的chkconfig方式**:在某个目录下创建脚本,如 `/root/hello/wei.sh`,脚本中必须加入特定注释,如 `#!/bin/bash`、`#chkconfig:2345 10 90`、`#description:resind` [^1]。 - **方法二:使用update-rc.d命令**:将 `disable_smt.sh` 脚本复制到 `/etc/init.d/` 目录中,使用 `update - rc.d` 命令将脚本添加到启动序列中,也可删除启动配置并停止脚本在启动时运行,还能查看服务列表和状态 [^2]。 - **方法三:创建启动文件方式(需用户登录后执行)**: 1. 创建脚本启动文件,如使用 `sudo vi auto - start - shell.desktop`,内容如下: ```plaintext [Desktop Entry] Encoding=UTF-8 Exec=/usr/bin/auto-start-shell.sh Type=Application Name=auto-start-shell ``` 2. 将shell脚本放到 `/usr/bin/`,脚本启动文件放到 `/etc/xdg/autostart/`: ```bash sudo cp -f auto-start-shell.sh /usr/bin/ sudo cp -f auto-start-shell.desktop /etc/xdg/autostart/ ``` 3. 重启系统三次,验证shell执行情况,如查看 `~/Desktop/ceshi/cs.txt` [^3]。 - **方法四:配置开机启动服务(默认以root用户执行)**: 1. 配置开机启动服务,使用 `sudo vim /etc/systemd/system/rc-local.service` 增加以下内容: ```plaintext [Install] WantedBy=multi-user.target Alias=rc-local.service ``` 2. 配置 `/etc/rc.local` 文件(不存在则手动创建),文件中配置需要执行的内容,如: ```bash #!/bin/bash /usr/bin/auto-start-shell.sh exit 0 ``` 3. 启动 `rc-local` 服务: ```bash sudo systemctl start rc-local sudo systemctl status rc-local ``` 4. 重启系统验证是否生效 [^3]。 - **方法五:使用systemd服务配置**:创建一个 `.service` 文件,如: ```plaintext [Unit] Description=My Custom Script After=network.target [Service] User=<your_username> # 使用实际用户名 ExecStart=/path/to/your/script.sh Restart=always # 根据需要选择重启策略,如不需要自动重启可省略或改为no [Install] WantedBy=multi-user.target ``` 然后使用 `systemctl enable` 命令设置开机自启,使用 `systemctl start` 命令单次启动,使用 `systemctl stop` 命令停止,使用 `systemctl status` 命令查看状态 [^4][^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值