31

I am trying to write my own (simple) systemd service which does something simple.( Like writing numbers 1 to 10 to a file, using the shell script). My service file looks like below.

[Unit]
Description=NandaGopal
Documentation=https://google.com
After=multi-user.target

[Service]
Type=forking  
RemainAfterExit=yes
ExecStart=/usr/bin/hello.sh &

[Install]
RequiredBy = multi-user.target

This is my shell script.

#!/usr/bin/env bash

source /etc/profile
a=0
while [ $a -lt 10 ]
do
   echo $a >> /var/log//t.txt
        a=`expr $a + 1`
done

For some reason, the service doesn't come up and systemctl is showing the below output.

root@TARGET:~ >systemctl status -l hello
* hello.service - NandaGopal
   Loaded: loaded (/usr/lib/systemd/system/hello.service; disabled; vendor     preset: enabled)
   Active: inactive (dead)
    Docs: https://google.com

Been trying to figure out what went wrong for the last 2 days.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
nandanator
  • 421
  • 1
  • 4
  • 5

2 Answers2

41
  • You have set Type=Forking, but your service doesn't work. Try Type=oneshot
  • You have a "&" your ExecStart line, which is not necessary.
  • The service is disabled, which means it was not enabled to start at boot. You should run systemctl enable hello to set it to start at boot.

You can check man systemd.directives to find an index of all the directives that you can use in your unit files.

heemayl
  • 39,294
  • 7
  • 70
  • 76
Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • Thank you so much for your help. [:)] Type=oneshot and the systemctl enable did the trick. I was using a simulator environment and when I created the symbolic link for the service, it worked smoothly. :) – nandanator Oct 06 '16 at 06:23
  • *"The service is disabled, which means it was not enabled to start at boot"* - That's not necessarily true. Systemd seems to sporadically lose configuration information, like setting a service or timer to enabled, so the timer or service starts when the machine starts. Also see [Systemd timer is lost after reboot?](https://unix.stackexchange.com/q/512105/56041) – jww Apr 12 '19 at 16:18
  • @PhilipRego You should create a new, complete question that states your complete problem. But use https://unix.stackexchange.com/ -- it's a better fit for `systemd` questions. – Mark Stosberg Nov 22 '19 at 22:27
9

Few points:

  1. If you use Type=forking, it is recommended to specify PidFile.

  2. In your case, Type=simple, and ExecStart without & will work.

  3. use systemctl start service-name to start a service

  4. Then use systemctl status service-name to check its status. status will be inactive/dead if service is not started.

Stefan Stoichev
  • 4,615
  • 3
  • 31
  • 51
Vadiraj S J
  • 639
  • 10
  • 17