10

I have a BASH-script I want to run on start up. My system is running systemd so I created a .service file with whith what I think is the neccessary information:

[Unit]
Description=My Script
After=network.target

[Service]
ExecStart=/home/myscript.sh

[Install]
WantedBy=multi-user.target

I used systemctl enable to 'register' it an rebooted. On boot I was told my script would be executed, but I could neither see any of the messages ECHO should display on screen nor did it write something to a file, according to what I had written in the script. Additionally, It does not start the application it's supposed to start.

Systemctl status tells me that the script has run and exited successfully. Still, the script has no effect. If I run the script from a shell it works perfectly fine.

Do any of you know what could be my problem?

3
  • 3
    Apparently, if your script starts something else it needs "Type=forking" I feel slightly stupid for not messing around with the settings earlier.
    – TokyoMEWS
    Commented Sep 13, 2012 at 14:13
  • 1
    +1 for solving it. Please post your solution as an answer and accept it to help other people with the same problem.
    – terdon
    Commented Sep 13, 2012 at 14:39
  • 1
    @TokyoMEWS: More precisely, if the script starts something else and exits while its children are running, it needs "Type=forking". Commented Sep 13, 2012 at 15:47

2 Answers 2

8

In addition to what TokyoMEWS found by themselves...

Apparently, if your script starts something else it needs "Type=forking"

(which is not completely correct – Type=forking only becomes necessary if your script exits while its children are running)

...other possible problems are:

  1. I'm guessing that by "display on screen" you meant that the script simply writes something to stdout. This does not go to the screen during boot – rather, everything from a service's stdout is sent to the journal (or to syslog depending on your systemd version).

  2. If you did actually attempt write to the screen (e.g. using echo Hi >/dev/tty1), then it's very likely that the script's output disappears when agetty clears the screen before showing login prompts. (To avoid that, you would have to order the unit [email protected]).

  3. To write something to a file, you need to have the file system mounted read-write. For this, After=local-fs.target might be necessary, otherwise the unit again might be started too early. But this depends on specific OS configuration.

1

If your script defines ExecStop clause as well, you need to set "RemainAfterExit=yes" to avoid ExecStop being called automatically after ExecStart. This will allow your service to be in status "active" after executing the ExecStart command.

[Service]
ExecStart=/home/myscript.sh start
ExecStop=/home/myscript.sh stop
RemainAfterExit=yes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .