I am still confused about systemd services being active vs the process still running vs not running etc.

Let's say I have the following basic shell script called my_script.sh :

touch /my_dir/my_file.txt
echo "Hello from my_script.sh" >> /my_dir/my_file.txt

I have a system service called my_service.service which calls my_script.sh as follows :

Description=my service 

ExecStart=sh /my_dir/my_script.sh


The fact that I have Restart=always , does this mean that as soon as my shell script finishes executing, it should execute again, and again, hence continuously writing to the file?

My understanding is that with Restart=always , the service is restarted regardless if there was a clean exit or not. This statement I don't understand properly. Will there be a clean exit after my_script.sh executes.

Now the line RemainAfterExit=yes , in my case, what is the effect of having this line defined or not?

Thanks in advance for clearing up any doubts.

  • The confusion might be because your script is not a service. A service continues to run after it is started. For example a listening service such as an SSH server. It runs in the background waiting for a service request. Your script doesn't do that. It runs and immediatly quits. When you think of 'restart' in the context of a service that is designed to run continuously, then it will probably make more sense to you. If a listening service such as SSH falls down then do we want to restart it or do we want to examine the reason for the crash? – Stephen Boston Jun 17 at 21:57
  • @StephenBoston In my case in my question, when my script finishes executing, should my service restart it ? I just can't seem to get this answer whatsoever – Engineer999 Jun 18 at 6:53
  • @StephenBoston In my case above, the script my_script.sh runs, then exits when finishes. However, the system service does not restart it again as I would expect having "Restart=always". This for me is confusing – Engineer999 Jun 18 at 7:27
  • Hm, I think I see now. Interesting. What happens if you put a long-running loop in your script, or set it to wait for input, something so that it continues to run. Then kill the process and see what happens. – Stephen Boston Jun 18 at 15:39

This line RemainAfterExit=yes mean service showed as active when script in ExecStart ends with success exit code.

This line Restart=always mean restart on each of next events:

  • Clean exit code or signal
  • Unclean exit code
  • Unclean signal
  • Timeout
  • Watchdog

If you need to not restart service on some exit codes, check this option: RestartPreventExitStatus=

More information about systemd.service there is

  • "Clean exit code or signal" - Is a clean exit when the script executes to completion? , hence my script creates the file, then writes to it and then has nothing else to do . I just can't seem to understand this part – Engineer999 Jun 17 at 21:15
  • @Engineer999: Why don't you try it? Execute your script from the command line and check its exit status with echo $? - what do you get? I get 0 which means that the script exited normally. – NickD Jun 17 at 21:22
  • @NickD That's when I run my script by itself yes. So my service should be restarting the script continuously? because it is not. I just can't seem to get a straight answer here – Engineer999 Jun 17 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.