CentOS7上建立自己的開機啟動服務

2021-10-24 13:25:50 字數 4278 閱讀 3604

本人有乙個自己寫的**用tcp**伺服器,需要隨系統啟動完成到 目標埠的**操作

命令為  tcp_proxy -l 20011 -r 182.61.145.146 -p 20011

建立服務指令碼檔案

vi   /usr/lib/systemd/system/wonrowl_proxy.service

寫入以下內容

[unit]

description=wonrowl proxy install as service test

after=network.target

[service]

type=forking

user=root

execstart=/root/wonrowl_proxy/start.sh

execstop=/root/wonrowl_proxy/stop.sh

[install]

wantedby=default.target

服務檔案中用到的兩個sh指令碼內容分別是

#!/bin/bash

/root/wonrowl_proxy/tcp_proxy -l 20011 -r 182.61.145.146 -p 20011 >> /dev/null & 

和#!/bin/bash

killall tcp_proxy

檔案許可權我為了省事,全部777

-rwxrwxrwx 1 root root  9038 oct  7 22:47 main.c

-rwxrwxrwx 1 root root    95 oct  8 14:56 start.sh

-rwxrwxrwx 1 root root    31 oct  8 14:55 stop.sh

-rwxrwxrwx 1 root root 14336 oct  7 22:47 tcp_proxy

萬事具備後

執行 systemctl start  wonrowl_proxy

即可啟動服務

通過 systemctl status wonrowl_proxy

檢視結果

● wonrowl_proxy.service - wonrowl proxy install as service test

loaded: loaded (/usr/lib/systemd/system/wonrowl_proxy.service; enabled; vendor preset: disabled)

active: active (running) since thu 2020-10-08 14:58:56 cst; 41s ago

process: 577 execstart=/root/wonrowl_proxy/start.sh (code=exited, status=0/success)

main pid: 580 (tcp_proxy)

cgroup: /system.slice/wonrowl_proxy.service

└─580 /root/wonrowl_proxy/tcp_proxy -l 20011 -r 182.61.145.146 -p 20011

oct 08 14:58:56 hecs-x-medium-2-linux-20201005162808 systemd[1]: starting wonrowl proxy install as service test...

oct 08 14:58:56 hecs-x-medium-2-linux-20201005162808 systemd[1]: started wonrowl proxy install as service test.

執行 systemctl stop wonrowl_proxy

即可關閉服務

通過 systemctl status wonrowl_proxy

[root@hecs-x-medium-2-linux-20201005162808 wonrowl_proxy]# systemctl status wonrowl_proxy

● wonrowl_proxy.service - wonrowl proxy install as service test

loaded: loaded (/usr/lib/systemd/system/wonrowl_proxy.service; enabled; vendor preset: disabled)

active: inactive (dead) since thu 2020-10-08 15:07:39 cst; 2s ago

process: 1566 execstop=/root/wonrowl_proxy/stop.sh (code=exited, status=0/success)

process: 577 execstart=/root/wonrowl_proxy/start.sh (code=exited, status=0/success)

main pid: 580 (code=killed, signal=term)

oct 08 14:58:56 hecs-x-medium-2-linux-20201005162808 systemd[1]: starting wonrowl proxy install as service test...

oct 08 14:58:56 hecs-x-medium-2-linux-20201005162808 systemd[1]: started wonrowl proxy install as service test.

oct 08 15:07:39 hecs-x-medium-2-linux-20201005162808 systemd[1]: stopping wonrowl proxy install as service test...

oct 08 15:07:39 hecs-x-medium-2-linux-20201005162808 systemd[1]: stopped wonrowl proxy install as service test.

此時雖然建立服務成功,但是並沒有具備開機自動的功能,通過 systemctl enable wonrowl_proxy  新增開機自啟動

[root@hecs-x-medium-2-linux-20201005162808 wonrowl_proxy]# systemctl enable wonrowl_proxy

created symlink from /etc/systemd/system/default.target.wants/wonrowl_proxy.service to /usr/lib/systemd/system/wonrowl_proxy.service.

直接reboot

重啟後  systemctl status wonrowl_proxy

[root@hecs-x-medium-2-linux-20201005162808 wonrowl_proxy]# systemctl status wonrowl_proxy

● wonrowl_proxy.service - wonrowl proxy install as service test

loaded: loaded (/usr/lib/systemd/system/wonrowl_proxy.service; enabled; vendor preset: disabled)

active: active (running) since thu 2020-10-08 15:09:40 cst; 4s ago

process: 1650 execstop=/root/wonrowl_proxy/stop.sh (code=exited, status=0/success)

process: 1658 execstart=/root/wonrowl_proxy/start.sh (code=exited, status=0/success)

main pid: 1659 (tcp_proxy)

cgroup: /system.slice/wonrowl_proxy.service

└─1659 /root/wonrowl_proxy/tcp_proxy -l 20011 -r 182.61.145.146 -p 20011

oct 08 15:09:40 hecs-x-medium-2-linux-20201005162808 systemd[1]: starting wonrowl proxy install as service test...

oct 08 15:09:40 hecs-x-medium-2-linux-20201005162808 systemd[1]: started wonrowl proxy install as service test.

可以看到已經自己執行,新增開機啟動成功

收工

centos7建立自己的yum源

安裝軟體 yum y install createrepo yum utils 設定yum源為阿里的映象 wget c etc yum.repo.d ali.repo wget c etc yum.repo.d epel.repo 同步阿里的映象到本地 a.建立資料夾 mkdir p home le...

centos7建立自己的yum源

安裝軟體 yum y install createrepo yum utils 設定yum源為阿里的映象 wget c etc yum.repo.d ali.repo wget c etc yum.repo.d epel.repo 同步阿里的映象到本地 a.建立資料夾 mkdir p home le...

Centos7開機啟動程式

1.自己新建乙個指令碼,如centnet service.sh mkdir centnet 建立檔案 centnet service.sh 經過後面的幾個步驟後,這個指令碼在開機的時候會執行,在這個指令碼裡面可以寫你開機的時候想執行的命令,如啟動tomcat,oracle等服務 2.在指令碼中輸入啟...