You are not logged in.

#1 2018-12-23 16:11:06

1ndeed
Member
Registered: 2018-09-28
Posts: 41

[SOLVED] Systemd can't execute the sleep command in shell script

I want to let my system automatically run a shell script when the system is booted . So I used systemctl to run it .
This is the systemd service file at /usr/lib/systemd/system/rc-local.service

[Unit]
Description="/etc/rc.d/* Compatibility" 

[Service]
Type=forking
ExecStart=sh /etc/rc.d/ddns.sh
TimeoutSec=0
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

And this is the file at /etc/rc.d/ddns.sh

#!/bin/bash

while true
do
	python /home/would/aliyun-ddns-client/ddns.py >> /dev/null 2>&1 &
	sleep 600
done

I actually don't quite understand the content at rc-local.service. I copied it from the internet . And I don't know if it is 100% suitable right here ...
But , It did work when I use the same way to auto run this script :

#!/bin/bash

sudo -u would transmission-daemon && sleep 5 && transmission-remote 9097 -n NAME:PASSWORD -tall -s

So I start to think , is it possible that in this way , systemd can not run the sleep command in shell script ?
Then I wrote this :

#!/bin/bash

while true
do
	echo "a" > /zzz/coming
	sleep 10
	echo "I was here" > /zzz/see.txt
	python /home/would/aliyun-ddns-client/ddns.py >> /dev/null 2>&1 &
	echo "I was here,too" > /zzz/zee.txt
	sleep 10
	echo "I was here as well" > /zzz/wee.txt
done

After reboot I checked /zzz :

[would@one zzz]$ sleep 30
[would@one zzz]$ ll
total 8
drwxrwxrwx  2 root  root  4096 Dec 24 00:01 ./
drwxr-xr-x 24 root  root  4096 Dec  8 11:49 ../
-rw-r--r--  1 would would    0 Dec 24 00:01 coming

Maybe I have made a bad mistake from the start , but I really need to know how to run this script properly . Correct me below , thanks .

Last edited by 1ndeed (2018-12-24 07:38:58)

Offline

#2 2018-12-23 16:30:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,531
Website

Re: [SOLVED] Systemd can't execute the sleep command in shell script

You set Type=forking.  Read the man page for what that means - your script doesn't fork.  Systemd likely kills the process after a short time (especially with TimeoutSec set to 0!)

As for the script it's crap to start with.  Why run a shell to run a python script which does something unspecified (likely just in turn run shell commands)?  Use one of the provided services:
https://wiki.archlinux.org/index.php/Dynamic_DNS

Alternatively, if this python script is actually good, use a systemd timer to launch it.  That's what timers are for, don't use a script with a loop and sleep.

EDIT: I just looked up that ddns tool, and it includes a proper service and timer:
https://github.com/rfancn/aliyun-ddns-client

Last edited by Trilby (2018-12-23 16:36:29)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2018-12-24 07:41:32

1ndeed
Member
Registered: 2018-09-28
Posts: 41

Re: [SOLVED] Systemd can't execute the sleep command in shell script

I was such a fool . I used exactly the same python script you posted , but I didn't check the .timer and .service file ... lol ...
Anyway , your thought is right . After reading wiki , I now wrote a ddns.timer file , And It's going well .
Thank you very much !!!

Offline

Board footer

Powered by FluxBB