Custom Systemd service – To run the script automatically during server boot up in CentOS7

Below are the steps to create custom systemd service

1) Create a file “hello.service” under directory either “/etc/systemd/system/” or “/usr/lib/systemd/system/” as below,

[root@centos7 ~]# cat /etc/systemd/system/hello.service
[Unit]
Description=hello service description
After=network.target

[Service]
Type=simple
ExecStart=/tmp/hello.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
[root@centos7 ~]#

 

[Unit] section may contain directive such as ‘ After=, Wants=, or Requires= directives’

[Service] section

TimeoutStartSec => systemd considers the service failed if a service doesn’t give start-up completion signal within TimeoutStartSec. For long-running shell scripts, we need to modify TimeoutStartSec accordingly. Here we put zero, it refers that we have disabled the timeout logic.

2) Create executable script ‘/tmp/hello.sh ‘ as mentioned in the ‘ExecStart=’ [Service] section

[root@centos7 ~]# cat /tmp/hello.sh
echo `date` > /root/helloworld.txt
[root@centos7 ~]#
[root@centos7 ~]# ls -lrt /tmp/hello.sh
-rwxr–r–. 1 root root 35 Feb 10 22:41 /tmp/hello.sh
[root@centos7 ~]#

3) Reload the systemd process

[root@centos7 ~]# systemctl daemon-reload

4) Enable the service

[root@centos7 ~]# systemctl enable hello.service
Created symlink from /etc/systemd/system/default.target.wants/hello.service to /etc/systemd/system/hello.service.
[root@centos7 ~]#

5) start the service and check the status,

[root@centos7 ~]# systemctl start hello.service
[root@centos7 ~]# systemctl status hello.service
● hello.service – hello service description
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sat 2018-02-10 22:56:31 EST; 5min ago
Process: 1265 ExecStart=/tmp/hello.sh (code=exited, status=203/EXEC)
Main PID: 1265 (code=exited, status=203/EXEC)

Feb 10 22:56:31 centos7.localdomain systemd[1]: Started hello service description.
Feb 10 22:56:31 centos7.localdomain systemd[1]: Starting hello service description…
Feb 10 22:56:31 centos7.localdomain systemd[1]: hello.service: main process exited, code=exited, status=203/EXEC
Feb 10 22:56:31 centos7.localdomain systemd[1]: Unit hello.service entered failed state.
Feb 10 22:56:31 centos7.localdomain systemd[1]: hello.service failed.
[root@centos7 ~]#

We are getting ‘failed’ status now.

After editing the script as below,

[root@centos7 ~]# cat /tmp/hello.sh
#!/usr/bin/bash
echo `date` > /root/helloworld.txt
[root@centos7 ~]#

[root@centos7 ~]# systemctl start hello.service
[root@centos7 ~]# systemctl status hello.service
● hello.service – hello service description
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2018-02-10 23:05:26 EST; 1s ago
Process: 1359 ExecStart=/tmp/hello.sh (code=exited, status=0/SUCCESS)
Main PID: 1359 (code=exited, status=0/SUCCESS)

Feb 10 23:05:26 centos7.localdomain systemd[1]: Started hello service description.
Feb 10 23:05:26 centos7.localdomain systemd[1]: Starting hello service description…
[root@centos7 ~]#

6) reboot the server to check whether hello.sh service executed or not

[root@centos7 ~]# reboot

 

###########
Before Reboot:
[root@centos7 ~]# pwd
/root
[root@centos7 ~]# ls -lrt
total 8
-rw——-. 1 root root 1457 Mar 12 2017 anaconda-ks.cfg
-rw-r–r–. 1 root root 29 Feb 10 23:05 helloworld.txt
[root@centos7 ~]# cat helloworld.txt
Sat Feb 10 23:05:26 EST 2018
[root@centos7 ~]# reboot

###########
After reboot:

[root@centos7 ~]# uptime
23:08:25 up 1 min, 1 user, load average: 0.50, 0.26, 0.10
[root@centos7 ~]# pwd
/root
[root@centos7 ~]# ls -lrt
total 8
-rw——-. 1 root root 1457 Mar 12 2017 anaconda-ks.cfg
-rw-r–r–. 1 root root 29 Feb 10 23:07 helloworld.txt
[root@centos7 ~]# cat helloworld.txt
Sat Feb 10 23:07:16 EST 2018
[root@centos7 ~]#

 

We could see helloworld.txt file is updated after the server reboot.