linux服务开机自启动之一(simple方式)

linux服务开机自启动之一(simple方式)

systemd服务方式

run.sh脚本

#!/bin/bash

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:../lib;export LD_LIBRARY_PATH
HOME=/home/gb;export HOME

if [ -x ${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr ]
then
        if [ "`ps -e -o comm=CMD | cut -f 1 -d' '| grep USB-Device-Mgr |grep -v grep  | sort | tail -1 `" != "USB-Device-Mgr" ]
        then
             echo "Start USB-Device-Mgr ..."
             ${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr
        fi
else
        echo "${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr is not executed"
fi

服务脚本

gb@gb:/etc$ su root
Password: 
root@gb:/etc# cd /etc/systemd/system
root@gb:/etc/systemd/system# ls -tlr
total 64
drwxr-xr-x 2 root root 4096 Sep 15  2021 getty.target.wants
lrwxrwxrwx 1 root root   44 Sep 15  2021 dbus-org.freedesktop.resolve1.service -> /lib/systemd/system/systemd-resolved.service
lrwxrwxrwx 1 root root   35 Sep 15  2021 syslog.service -> /lib/systemd/system/rsyslog.service
lrwxrwxrwx 1 root root   38 Sep 15  2021 iscsi.service -> /lib/systemd/system/open-iscsi.service
drwxr-xr-x 2 root root 4096 Sep 15  2021 graphical.target.wants
lrwxrwxrwx 1 root root   41 Sep 15  2021 vmtoolsd.service -> /lib/systemd/system/open-vm-tools.service
drwxr-xr-x 2 root root 4096 Sep 15  2021 open-vm-tools.service.requires
drwxr-xr-x 2 root root 4096 Sep 15  2021 paths.target.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 timers.target.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 cloud-final.service.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 final.target.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 sysinit.target.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 sockets.target.wants
drwxr-xr-x 2 root root 4096 Sep 15  2021 network-online.target.wants
lrwxrwxrwx 1 root root   36 Jun 29 02:02 dbus-org.freedesktop.thermald.service -> /lib/systemd/system/thermald.service
lrwxrwxrwx 1 root root   31 Jun 29 02:03 sshd.service -> /lib/systemd/system/ssh.service
drwxr-xr-x 2 root root 4096 Jun 29 02:06 sshd-keygen@.service.d
drwxr-xr-x 2 root root 4096 Jun 29 02:06 cloud-init.target.wants
-rw-rw-r-- 1 gb   gb    149 Jun 29 02:23 jzd_drv.service
-rw-r--r-- 1 root root  222 Jul 30 03:34 gb_usbdevicemgr.service
drwxr-xr-x 2 root root 4096 Jul 30 03:34 default.target.wants
drwxr-xr-x 2 root root 4096 Jul 30 03:34 multi-user.target.wants
root@gb:/etc/systemd/system# vim gb_usbdevicemgr.service 

[Unit]
Description=My Service
After=network.target

[Service]

ExecStart=/home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/run.sh
WorkingDirectory=/home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/
Restart=always

[Install]
WantedBy=multi-user.target

注:Restart=always,在程序USB-Device-Mgr被kill掉时,服务将自动拉起USB-Device-Mgr

更新配置

root@gb:/etc/systemd/system#systemctl daemon-reload 

启动服务

root@gb:/etc/systemd/system# 
root@gb:/etc/systemd/system# systemctl enable gb_usbdevicemgr.service 
Created symlink /etc/systemd/system/multi-user.target.wants/gb_usbdevicemgr.service → /etc/systemd/system/gb_usbdevicemgr.service.
root@gb:/etc/systemd/system# systemctl start gb_usbdevicemgr.service 
root      1070     1  0 06:43 ?        00:00:00 /bin/bash /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/run.sh
root      1205  1070  0 06:43 ?        00:00:12 /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr
root      2045  1796  0 11:18 pts/0    00:00:00 grep --color=auto USB-Device-Mgr
root@gb:/etc/systemd/system# 

查看服务

root@gb:/etc/systemd/system# systemctl status gb_usbdevicemgr.service
● gb_usbdevicemgr.service - My Service
   Loaded: loaded (/etc/systemd/system/gb_usbdevicemgr.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2025-01-07 06:42:32 UTC; 4h 35min ago
 Main PID: 1070 (run.sh)
    Tasks: 57 (limit: 4915)
   CGroup: /system.slice/gb_usbdevicemgr.service
           ├─1070 /bin/bash /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/run.sh
           └─1205 /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr

Jan 07 06:42:32 gb systemd[1]: Started My Service.
Jan 07 06:42:32 gb run.sh[1070]: Start USB-Device-Mgr ...
root@gb:/etc/systemd/system# 

停止服务

root@gb:/etc/systemd/system# ps -ef |grep USB-Device-Mgr
root      1070     1  0 06:43 ?        00:00:00 /bin/bash /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/run.sh
root      1205  1070  0 06:43 ?        00:00:12 /home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr
root      2045  1796  0 11:18 pts/0    00:00:00 grep --color=auto USB-Device-Mgr
root@gb:/etc/systemd/system# 
root@gb:/etc/systemd/system# systemctl stop gb_usbdevicemgr.service 
root@gb:/etc/systemd/system# ps -ef |grep USB-Device-Mgr
root      2124  1753  0 04:35 pts/0    00:00:00 grep --color=auto USB-Device-Mgr
root@gb:/etc/systemd/system# 

注:stop服务时,不但停止run.sh,还会将run.sh拉起来的USB-Device-Mgr停掉

查看服务日志

root@gb:/etc/systemd/system# journalctl -u gb_usbdevicemgr.service 
Jan 07 06:42:32 gb systemd[1]: Started My Service.
Jan 07 06:42:32 gb run.sh[1070]: Start USB-Device-Mgr ...

参考文献:

Linux 应用开机自启 详解_linux 开机自启动-优快云博客

Linux 设置开机自启动_linux_mayue_csdn-GitCode 开源社区

Linux - 开机启动流程_linux开机自启过程-优快云博客

linux的.service文件配置详解

Linux 系统设置开机自动运行脚本的方法

ubuntu没有rc.local文件

Ubuntu 20.04 手动实现 rc.local

ubuntu设置程序开机自启动

Linux Nginx 服务设置开机自启动

官方文档
systemctl服务文件管理指南
systemd 服务脚本编写和管理
使用systemd 脚本设置服务keepalive

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值