发布于 2015-11-09 14:40:52 | 206 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的精品教程,程序狗速度看过来!

CentOS Linux操作系统

CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成。


编写一个无线循环的脚本, 并且将输出写入到/var/log/helloworld.log文件中.

[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

编写一个系统服务文件

[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

设置成为开机自启动服务

[root@localhost 20151109]# systemctl enable helloworld

启动helloworld服务

[root@localhost 20151109]# systemctl enable helloworld

观察日志信息

[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服务

[root@localhost 20151109]# systemctl stop helloworld

查看报错信息:

[root@localhost 20151109]# systemctl status helloworld

报错信息:

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

[emerg] 254#0: open() "xxx" failed (13: Permission denied)
表示文件没有运行权限.(chmod +x 程序文件名)


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务