Search code examples
centos7systemctl

Create a service using systemctl on CentOS 7


I'm trying to create a service using systemctl on my CentOS 7 server.

In /usr/lib/systemd/system I've this situation ...

[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]# 

If I try to execute

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

all works fine.

In /etc/systemd/system I've this ....

[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]#

When I try to execute

systemctl enable getUpdatesTelegram.service

I obtain

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

The log I've is tthe follow ....

[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.

Where I'm wrong?

Thank you in advance!


Solution

  • First, custom services should not be put in /usr/lib/systemd/system, but in /etc/systemd/system. Same goes with shell scripts, just put them into /usr/local/sbin or something.

    The reason why this does not work is that the script path in your service file getUpdatesTelegram.service is wrong:

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

    should really be

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

    (Though as I wrote, don't put shell scripts into /usr/lib/systemd/system)