cacophony555
Posts: 140
Joined: Sat Jan 18, 2014 5:54 pm

Unable to start service within script file initiated by cron

Fri May 08, 2015 11:50 pm

I've noticed that deluged has a tendency to crash so I was interested in monitoring the service and restarting if necessary via cron, but I can't get it to work.

Here's the cron job:

Code: Select all

* * * * * /home/pi/scripts/deluged-watcher.sh
I've verified that the cron job is firing and that the logic in the script works as intended (ie. if I modify the script to write a string to a file instead of the line below I see that occur fine), but the critical line never works. Here was what that line was initially:

Code: Select all

sudo service deluge-daemon restart &
I realized that sudo probably isn't allowed in the script, but the service command requires root permissions. So I tried removing sudo from the line above and instead ran the cron job as root by adding it this way:

Code: Select all

sudo crontab -e -u root
That also didn't work. So then I found out about cron not knowing certain paths, and tried modifications to that line of the script such as:

Code: Select all

/usr/sbin/service deluge-daemon restart &
and

Code: Select all

 /etc/init.d/deluge-daemon restart &
Neither worked. I also tried adding "sudo" to the cron job, even though it's already supposed to be running as root:

Code: Select all

* * * * * sudo /home/pi/scripts/deluged-watcher.sh
Not sure what else to try.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Unable to start service within script file initiated by

Sat May 09, 2015 12:52 am

cron entries already run with root privileges. Is it possible that the service is restarting and then immediately crashing again?
marcus
:ugeek:

cacophony555
Posts: 140
Joined: Sat Jan 18, 2014 5:54 pm

Re: Unable to start service within script file initiated by

Sat May 09, 2015 12:54 am

hmmm... looks like there is an issue with the logic.

I don't understand why this isn't working. The full output from my console shows what I'm seeing:

Code: Select all

pi@raspberrypi ~/scripts $ more deluged-watcher.sh
#!/bin/bash

# Check if deluged is running
if pgrep "deluged" > /dev/null
then
    echo "Running"
else
    echo "Date: " $(date) >> /home/pi/scripts/deluged.log
    /usr/sbin/service deluge-daemon restart >>/home/pi/scripts/output.txt 2>&1
fi
pi@raspberrypi ~/scripts $ pgrep "deluged"
pi@raspberrypi ~/scripts $ ./deluged-watcher.sh
Running
pi@raspberrypi ~/scripts $ pgrep "deluged"
As shown above, the script thinks the process is running even though it's not.

I then tried removing the redirect to /dev/null as shown below:

Code: Select all

pi@raspberrypi ~/scripts $ more deluged-watcher.sh
#!/bin/bash

# Check if deluged is running
if pgrep "deluged"
then
    echo "Running"
else
    echo "Date: " $(date) >> /home/pi/scripts/deluged.log
    /usr/sbin/service deluge-daemon restart >>/home/pi/scripts/output.txt 2>&1
fi
pi@raspberrypi ~/scripts $ pgrep "deluged"
pi@raspberrypi ~/scripts $ ./deluged-watcher.sh
7342
Running
pi@raspberrypi ~/scripts $ ./deluged-watcher.sh
7344
Running
And it's showing a different process id each time.

cacophony555
Posts: 140
Joined: Sat Jan 18, 2014 5:54 pm

Re: Unable to start service within script file initiated by

Sat May 09, 2015 4:43 am

I figured it out. The watcher script I wrote had "deluged" as part of the title, so every time the script checked if "deluged" was running... it thought it was!

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Unable to start service within script file initiated by

Sat May 09, 2015 4:52 am

... oh, gawd... I love it.
marcus
:ugeek:

cacophony555
Posts: 140
Joined: Sat Jan 18, 2014 5:54 pm

Re: Unable to start service within script file initiated by

Sat May 09, 2015 5:04 am

What I really should have used is:

pgrep --exact "deluged"

Then I would have been fine with the "deluged-watcher.sh" script name

:D

Return to “Troubleshooting”