Running A Service On RHEL / CentOS

Ong Chao Jian
2 min readJan 5, 2021

--

Running a service is useful when you need to run tasks without human intervention, letting the machine takes care of spawning the job and keeping it alive.

Let’s try and create a service for the script below.

First, we will need to create a service script as shown below.

Second, you will need to change the “WorkDirectory” in the service script to a location where your shell script assumes where its working directory is located. Otherwise, it will be the location where the shell script is located.
The parameter “User” also has to be a user that has execute permission over the script. You can check and change the permission and ownership of the file using the example below.

Note: In case you may want to run a jar file or nodejs file, you can use the command below to find out where the java or nodejs library directory is located.

Then change the “ExecStart” using the results above as the first parameter and your jar file or nodejs file as the second parameter. Your service script will look something like the service script below.

Third, you will need to place this service script within the systemd directory.

Forth, you will need to enable and start the service.

After checking the status of the service, and should you want more details of the service or to debug it, you can view the logs using the command below.

If you like my notes, especially if it saves you some time pulling your hair out, feel free to give me a clap or even buy me a donut ;)

--

--

Ong Chao Jian
Ong Chao Jian

Written by Ong Chao Jian

0 Followers

happily married with a kid • loves fresh morning air • working towards a better version of myself

No responses yet