folibis
folibis

Reputation: 12854

systemd doesn't run an application from bash script

I have a service that should run set of applications in background on my Yocto embedded Linux system. I don't like an idea to create a systemd startup script for each app so I just run them from a bash script as following:

The service:

startup.service

[Unit]
Description=applications startup script
After=network.target

[Service]
Type=simple
ExecStart=/opt/somedir/startup.sh

[Install]
WantedBy=multi-user.target

and the script

startup.sh

#!/bin/bash

echo "application startup script"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/somedir
/opt/somedir/app1 &
/opt/somedir/app2 &
/opt/somedir/app3 &

But no application started. Checking the service status give me:

systemctl status startup

● startup.service - applications startup script
   Loaded: loaded (/lib/systemd/system/startup.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2021-03-25 10:33:16 UTC; 18min ago
  Process: 428 ExecStart=/opt/somedir/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 428 (code=exited, status=0/SUCCESS)

Mar 25 10:33:16 systemd[1]: Started application startup script.
Mar 25 10:33:16 startup.sh[428]: application startup script
Mar 25 10:33:16 systemd[1]: startup.service: Succeeded.

So the service executed on the system startup and executes the script. If I execute the script from the command line it starts the applications as expected. So what a reason that no application run?

Upvotes: 1

Views: 5049

Answers (2)

Raman Sailopal
Raman Sailopal

Reputation: 12867

Systemd will need to know how to run the script. Therefore either add:

#!/bin/bash

to the top line of the startup.sh script or change the ExecStart line in the systemd service file to:

ExecStart=/bin/bash -c /opt/somedir/startup.sh

Also, to ensure that the processes spawned remain persistent after being spawned, change:

Type=forking

Upvotes: 5

meuh
meuh

Reputation: 12255

systemd runs script startup.sh, and after that process ends, it assumes all is done so it kills off any remaining processes and the unit ends. The simplest solution is to add a wait at the end of startup.sh so that it only returns when the backgrounded processes have all ended.

Upvotes: 1

Related Questions