How to write startup script for systemd?
systemd
is the latest service management utility
in all latest version of Linux distribution such as Ubuntu OS, Redhat OS, CentOS.
That’s why Ubuntu 17.4 and Redhat 7.4 version now supports systemctl command
as an upstart script. If you are on older version of Linux OS then you may need to look at bottom of this page to see older init.d script
🙂
We are going to discuss below things in this tutorial
- Setting up systemd service unit
- How to
configure
systemd? - How to
enable
it systemd? - How start process automatically using systemd?
- How to stop and
respawn
process using systemd?
Setup Steps for this tutorial:
Prerequisite:
We have compiled version of CrunchifyAlwaysRunningProgram.java program at /tmp/crunchify
folder. This java process will keep running in background forever. You could download program from here.
Once you download it then try to compile
it and run it to make sure it’s working.
root@crunchify:/tmp/crunchify# ls -ltr -rw-r--r-- 1 root root 120 Oct 6 17:57 crunchify.tar.gz drwxr-xr-x 2 root root 4096 Oct 6 18:01 tomcat drwxr-xr-x 2 root root 4096 Oct 6 18:02 java -rw-r--r-- 1 root root 621 Oct 7 16:06 package -rwxrwxrwx 1 root root 838 Oct 7 16:07 CrunchifyAlwaysRunningProgram.java -rwxrwxrwx 1 root root 1295 Oct 8 14:38 CrunchifyAlwaysRunningProgram.class root@crunchify:/tmp/crunchify# javac CrunchifyAlwaysRunningProgram.java root@crunchify:/tmp/crunchify# java CrunchifyAlwaysRunningProgram always running program ==> Sun Oct 08 14:39:14 UTC 2017 always running program ==> Sun Oct 08 14:39:16 UTC 2017 ... ... ...
You can type CTRL + C
to quit program.
Now let’s get started on setting up systemd auto startup script for above program.
Step-1
- Go to folder /lib/systemd/system
- Type
cd /lib/systemd/system
Step-2
- create file
crunchify.service
(change filename accordingly) - put below content into it
[Unit] Description=Crunchify Java Process Restart Upstart Script After=auditd.service systemd-user-sessions.service time-sync.target [Service] User=root TimeoutStartSec=0 Type=simple KillMode=process export JAVA_HOME=/opt/java/jdk-9 export PATH=$PATH:$JAVA_HOME/bin WorkingDirectory=/tmp/crunchify ExecStart=/opt/java/jdk-9/bin/java -cp /tmp/crunchify CrunchifyAlwaysRunningProgram Restart=always RestartSec=2 LimitNOFILE=5555 [Install] WantedBy=multi-user.target
Step-3
- Save file
- Provide execute permission using
chmod 755 crunchify.service
Step-4
- Load crunchify.service using command
systemctl daemon-reload
- Enable crunchify.service using command
systemctl enable crunchify.service
- Start crunchify.service using command
systemctl start crunchify
- Check status of crunchify.service using command
systemctl status crunchify
Step-5
- Now check if process is running or not??
- Type command
ps -few | grep java
to see all running Java processes - You should see something like this
root@crunchify:/lib/systemd/system# ps -few | grep java root 28631 1 0 14:02 ? 00:00:03 /opt/java/jdk-9/bin/java -cp /tmp/crunchify CrunchifyAlwaysRunningProgram root 28853 28226 0 14:46 pts/0 00:00:00 grep java
That means process 28631
is starting using your upstart script. You may have different process ID.
How to verify if your systemd crunchify.service is working or not?
- Try to kill process using
kill -9 28631
and you should see new process should be created automatically. - In my case new process ID
28887
created 🙂
root@crunchify:/lib/systemd/system# kill -9 28631 root@crunchify:/lib/systemd/system# ps -few | grep java root 28887 1 17 14:48 ? 00:00:00 /opt/java/jdk-9/bin/java -cp /tmp/crunchify CrunchifyAlwaysRunningProgram root 28900 28226 0 14:48 pts/0 00:00:00 grep java
Video: Complete Live systemd upstart script setup example:
This video contains – How to Setup systemd startup/upstart script in CentOS, Redhat, Ubuntu Linux OS?
- How to automatically execute shell script at startup boot
- How To Create a systemd Service in Linux (CentOS 7)
- How To Configure a Linux Service to Start automatically?
- Centos 7 systemd startup script
- Ubuntu systemd startup script
Want to learn more on systemd? Here is a cheat sheet for your reference.
I hope you learn everything about systemd command
and setting up upstart script on all types of Linux OS.
STOP: For Older Version of OS ONLY 🙂
If you are running older version
of Redhat, Ubuntu, CentOS version then you will need to have script in this format.
- Go to
/etc/init
- Create file
crunchify.conf
# Run Crunchify's Java program indefinitely # description "Crunchify Java Process Restart Upstart Script" author "Crunchify.com (App Shah)" start on runlevel [2345] stop on runlevel [!2345] respawn respawn limit 10 5 script su - root << 'EOF' export JAVA_HOME=/opt/java/jdk-9 export PATH=$PATH:$JAVA_HOME/bin exec /opt/java/jdk-9/bin/java -cp /tmp/crunchify CrunchifyAlwaysRunningProgram EOF end script
- Just start service using command
service crunchify start
- Follow the same verification steps as above
Let me know if you see any issue running upstart script in your environment. Enjoy your day and Happy coding.