在CentOS 7上使用systemctl创建服务

时间:2018-10-24 20:47:16

标签: centos7 systemctl

我正在尝试在CentOS 7服务器上使用systemctl创建服务。

在/ usr / lib / systemd / system中,我遇到这种情况...

[root@centos-4gb-hel1-1 system]# pwd
/usr/lib/systemd/system
[root@centos-4gb-hel1-1 system]# ls -la getUpdatesTelegram.sh
-rwxr-xr-x. 1 root root 116 Oct 24 22:32 getUpdatesTelegram.sh
[root@centos-4gb-hel1-1 system]# cat getUpdatesTelegram.sh
#!/bin/bash
while true ; do
   /var/www/html/OpenProntoSoccorsi/TelegramBot/getUpdates_launcher.sh
   sleep 5
done
[root@centos-4gb-hel1-1 system]# 

如果我尝试执行

   /var/www/html/OpenProntoSoccorsi/TelegramBot/getUpdates_launcher.sh

一切正常。

在/ etc / systemd / system中,我有这个...。

[root@centos-4gb-hel1-1 system]# cd /etc/systemd/system
[root@centos-4gb-hel1-1 system]# ls -la
total 40
drwxr-xr-x. 10 root root 4096 Oct 24 22:05 .
drwxr-xr-x.  4 root root 4096 Oct 18 22:41 ..
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 basic.target.wants
lrwxrwxrwx.  1 root root   37 Sep 24 12:29 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 default.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:27 dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 getty.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 local-fs.target.wants
drwxr-xr-x.  2 root root 4096 Oct 24 22:32 multi-user.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 sysinit.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 system-update.target.wants
[root@centos-4gb-hel1-1 system]# cd multi-user.target.wants/
[root@centos-4gb-hel1-1 multi-user.target.wants]# ls -la
total 8
drwxr-xr-x.  2 root root 4096 Oct 24 22:32 .
drwxr-xr-x. 10 root root 4096 Oct 24 22:05 ..
lrwxrwxrwx.  1 root root   38 Sep 24 12:27 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:31 brandbot.path -> /usr/lib/systemd/system/brandbot.path
lrwxrwxrwx.  1 root root   39 Sep 24 12:27 chronyd.service -> /usr/lib/systemd/system/chronyd.service
lrwxrwxrwx.  1 root root   44 Sep 24 12:33 cloud-config.service -> /usr/lib/systemd/system/cloud-config.service
lrwxrwxrwx.  1 root root   43 Sep 24 12:33 cloud-final.service -> /usr/lib/systemd/system/cloud-final.service
lrwxrwxrwx.  1 root root   48 Sep 24 12:33 cloud-init-local.service -> /usr/lib/systemd/system/cloud-init-local.service
lrwxrwxrwx.  1 root root   42 Sep 24 12:33 cloud-init.service -> /usr/lib/systemd/system/cloud-init.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:26 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx.  1 root root   50 Oct 24 22:32 getUpdatesTelegram.service -> /usr/lib/systemd/system/getUpdatesTelegram.service
lrwxrwxrwx.  1 root root   42 Sep 24 12:27 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:29 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx.  1 root root   40 Sep 24 12:26 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx.  1 root root   46 Sep 24 12:31 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx.  1 root root   39 Sep 24 12:26 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx.  1 root root   36 Sep 24 12:27 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:26 tuned.service -> /usr/lib/systemd/system/tuned.service
[root@centos-4gb-hel1-1 multi-user.target.wants]# cat getUpdatesTelegram.service
[Service]
Type=simple
ExecStart=/bin/bash /bin/getUpdatesTelegram.sh

[Install]
WantedBy=multi-user.target
[root@centos-4gb-hel1-1 multi-user.target.wants]#

当我尝试执行

systemctl enable getUpdatesTelegram.service

我获得

...
...
getUpdatesTelegram.service  loaded failed failed   getUpdatesTelegram.service
...
...

我的日志如下...

[root@centos-4gb-hel1-1 multi-user.target.wants]# journalctl -u getUpdatesTelegram.service
-- Logs begin at Fri 2018-10-19 20:09:02 CEST, end at Wed 2018-10-24 22:44:11 CEST. --
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Started getUpdatesTelegram.service.
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Starting getUpdatesTelegram.service...
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: getUpdatesTelegram.service: main process exited, code=exited, status=203/EXEC
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Unit getUpdatesTelegram.service entered failed state.
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: getUpdatesTelegram.service failed.

我错了吗?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

首先,自定义服务不应放在/usr/lib/systemd/system中,而应放在/etc/systemd/system中。 Shell脚本也是如此,只需将它们放入/usr/local/sbin或其他内容中即可。

之所以不起作用,是因为服务文件getUpdatesTelegram.service中的脚本路径错误:

ExecStart=/bin/bash /bin/getUpdatesTelegram.sh

应该是

ExecStart=/bin/bash /usr/lib/systemd/system/getUpdatesTelegram.sh

(尽管如我所写,请勿将shell脚本放入/usr/lib/systemd/system

答案 1 :(得分:1)

作为Red Hat分支,CentOS倾向于遵循类似的准则。

Red Hat system administrator's guide为您的情况提供了this有用的段落。

Jakob提出的解决方案是正确的。