編寫簡單的CentOS7系統服務文件

編寫一個無線循環的腳本, 而且將輸出寫入到/var/log/helloworld.log文件中.
shell

[root@localhost ~]# mkdir /home/zhengtong/20151109/
[root@localhost ~]# cd /home/zhengtong/20151109/
[root@localhost 20151109]# vim helloworld.sh
#!/bin/bash
# __author__ = 'zhengtong'

while [ : ]
do
    echo $(date "+%Y-%m-%d %H:%M:%S") 'hello world!' >> /var/log/helloworld.log 2>&1
    sleep 1
done
[root@localhost 20151109]# chmod +x helloworld.sh


編寫一個系統服務文件vim

[root@localhost 20151109]# vim /usr/lib/systemd/system/helloworld.service
[Unit]
Description=helloworld service
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/home/zhengtong/20151109/helloworld.sh
ExecStop=/bin/kill -9 $MAINPID

[Install]
WantedBy=multi-user.target


設置成爲開機自啓動服務bash

[root@localhost 20151109]# systemctl enable helloworld


啓動helloworld服務日誌

[root@localhost 20151109]# systemctl start helloworld


觀察日誌信息code

[root@localhost 20151109]# tail -f /var/log/helloworld.log
2015-11-09 04:01:09 hello world!
2015-11-09 04:01:10 hello world!
2015-11-09 04:01:11 hello world!
2015-11-09 04:01:12 hello world!
2015-11-09 04:01:13 hello world!
2015-11-09 04:01:14 hello world!


中止helloworld服務ip

[root@localhost 20151109]# systemctl stop helloworld


查看報錯信息:rem

[root@localhost 20151109]# systemctl status helloworld


報錯信息:get

main process exited, code=exited, status=2/INVALIDARGUMENT
表示ExecStart寫的不正確.

[emerg] 254#0: open() "xxx" failed (13: Permission denied)
表示文件沒有運行權限.(chmod +x 程序文件名)
相關文章
相關標籤/搜索