I got a bash script essentially runnning this:
#!/bin/bash
[...]
while true; do
str="broadcast "`randArrayElement "messages[@]"`
server_send_message $str
sleep $interval
done
Now I want to run this as a systemd service, my service script looks like this:
[Unit]
Description=Announcer
After=network.target
[Service]
ExecStart=/usr/local/bin/somescript &; disown
ExecStop=/usr/bin/kill -9 `cat /tmp/somescript.pid`
Type=forking
PIDFile=/tmp/somescript.pid
[Install]
WantedBy=default.target
Unfortunately when I run this service via service somescript start
it is working but because of the while true loop my terminal is stuck in starting the script:
● somescript.service - somescript service
Loaded: loaded (/etc/systemd/system/somescript.service; disabled; vendor preset: enabled)
Active: activating (start) since Wed 2016-08-17 12:22:34 CEST; 43s ago
Control: 17395 (somescript)
CGroup: /system.slice/somescript.service
├─17395 /bin/bash /usr/local/bin/somescript &; disown
└─17409 sleep 600
How can I run this script as a service without being stuck in "starting" / the while true loop?
Best Answer
You need to let systemd work for you. Let it handle the forking at the start and the killing of the process. Eg replace your service part by
then you can use
systemctl start
,status
andstop
. You must remember that the lines in systemd are NOT interpreted by the shell, so for example your&;
is merely passed as another parameter of 2 characters to your script.