LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2014, 06:41 AM   #1
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Rep: Reputation: Disabled
Cronjob in Raspbian failing


Hi wizards!
I'm going a bit instane, and hope one of you guys could help.
I'm running Deluge on a Raspberry Pi, but it's not the perfect platform for it. Deluge keeps crashing, so I've made a cronjob that checks if it's running, and is supposed to restart it.
So far so good. The script, when executed from bash, does the job.
However, when it's executed from my crontab, doesn't start Deluge.
I've written some logging into the script, and all seems well. But no Deluge... Plz tell me if I'm doing something silly/wrong...

Here's the script:


Code:
#!/bin/bash
#deluge-checker.sh
#make sure deluge is running

export DISPLAY=:0 #needed if you are running a simple gui app.

process=deluged
makerun="service deluge start"


if ps ax | grep -v grep | grep $process > /dev/null
        then

		echo 'deluged process running - no restart required'
                exit

        else

        $makerun &
		echo 'deluged process restarted' &
		echo "Exit status:" $? >> /home/pi/scripts/deluged.log
		echo "Date: " $(date) >> /home/pi/scripts/deluged.log

        fi

exit
The script logs exit status as 0, no matter if cron or bash executes it, and it logs the time as well. It just doesn't start Deluge.

<3
/sverker, Malmö

Last edited by monkeyslut; 09-18-2014 at 06:43 AM.
 
Old 09-18-2014, 08:32 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
cron even when run as the same user you ran as from command line doesn't have the same environment because it doesn't process profiles and bashrc files.

This means any script intended to run from cron must include environmental variables for the things it needs.

The most common cause of issues is that you are doing commands that simply aren't found because the PATH variable is minimal.

In your script you are running "ps" and "service". You should either specify the full path to these commands (e.g. /bin/ps, /sbin/service) OR add the directories to a PATH declaration before you run the command:
e.g. export PATH=$PATH:/bin:/sbin
That tells it to add the two directories to whatever PATH was already defined. (Typically /bin is there but /sbin might not be.)

To determine the path to commands you're using from command line just type "which <command>".
 
1 members found this post helpful.
Old 09-18-2014, 08:55 AM   #3
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Roger, thanks. I've changed the code, and it now states the full path to "ps" and "service" - but still no go.
And, still: the echo lines inside the if statement still works, so the script runs successfully from cron. It's just the starting up bit that doesn't kick.'

the script now reads:

Code:
#!/bin/bash
#deluge-checker.sh
#make sure deluge is running

export DISPLAY=:0 #needed if you are running a simple gui app.

process=deluged
makerun="/usr/sbin/service deluge start"


if /bin/ps ax | grep -v grep | grep $process > /dev/null
        then
		echo 'deluged process running - no restart required'
                exit
        else
        $makerun &
		echo 'deluged process restarted' &
		echo "Exit status:" $? >> /home/pi/scripts/deluged.log
		echo "Date: " $(date) >> /home/pi/scripts/deluged.log
        fi
exit

Last edited by monkeyslut; 09-18-2014 at 08:58 AM.
 
Old 09-18-2014, 09:25 AM   #4
Soadyheid
Senior Member
 
Registered: Aug 2010
Location: Near Edinburgh, Scotland
Distribution: Cinnamon Mint 17.3 and 18.2 at present.
Posts: 1,280

Rep: Reputation: 291Reputation: 291Reputation: 291
I may be talking rubbish but as the Raspberry Pi doesn't have an RTC would that prevent cron jobs running? Or have you got some software generated clock running?

My

Play Bonny!

 
Old 09-19-2014, 04:15 AM   #5
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
It has a software clock, and the job runs on the minute as it should.

Last edited by monkeyslut; 09-19-2014 at 04:19 AM.
 
1 members found this post helpful.
Old 09-19-2014, 07:55 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
Is /etc/init.d/deluge linked to whatever your default run level (/etc/rc?.d where the question is the run level number)? If so does deluge start properly after a reboot of the system?

I'm not familiar with deluge and am wonder if perhaps it needs a terminal present to run. You'd have that at command line but wouldn't in cron. There are work arounds for that.

BTW: The "echo" works because it is a bash built in command and your first (interpreter) line tells it to use bash. That is why I didn't mention needing to find its path. Note that there IS an "echo" binary available as well and on occasion you have to specify full path to that (e.g. when using "echo -e") but for the most part the bash built in "echo" is sufficient.
 
1 members found this post helpful.
Old 09-19-2014, 05:02 PM   #7
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Snap, I haven't even thought of reviewing the deluge script. I'll have to do that when I get home, I'm at work ATM.
However: deluge does start properly at startup.

Edit:
Nah, I can't find much in the deluge script that doesn't fit. Here's the code for it...

Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          deluged
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start deluged at boot time
# Description:       Start DelugeD and Deluge-Web.
### END INIT INFO

#Author              QuidsUp
#Last Edited	     26 Jan 2013

#Required -- To be changed!
USER=pi        #Set Username here.
PORT=8112            #Set port number for web interface (default 8112)
PIDFileDeluge=/home/pi/.config/deluge/deluged.pid
PIDFileWeb=/home/pi/.config/deluge/deluge-web.pid

case "$1" in
  start)
    echo "Starting Deluge"
    start-stop-daemon --start --chuid $USER --name deluged --pidfile $PIDFileDeluge \
     --background --make-pidfile --exec /usr/bin/deluged -- --do-not-daemonize 
    start-stop-daemon --start --chuid $USER --name deluge-web --pidfile $PIDFileWeb \
     --background --make-pidfile --exec /usr/bin/deluge-web -- --port $PORT
  ;;

  stop)
    echo "Stopping Deluge"
    start-stop-daemon --stop --chuid $USER --name deluged --pidfile $PIDFileDeluge
    start-stop-daemon --stop --chuid $USER --name deluge-web --pidfile $PIDFileWeb
  ;;

  restart|force-reload)
    echo "Stopping Deluge for Restart"
    start-stop-daemon --stop --chuid $USER --name deluged --pidfile $PIDFileDeluge
    start-stop-daemon --stop --chuid $USER --name deluge-web --pidfile $PIDFileWeb
    sleep 5s
    echo "Restarting Deluge"
    start-stop-daemon --start --chuid $USER --name deluged --pidfile $PIDFileDeluge \
      --background --make-pidfile --exec /usr/bin/deluged -- --do-not-daemonize 
    start-stop-daemon --start --chuid $USER --name deluge-web --pidfile $PIDFileWeb \
      --background --make-pidfile --exec /usr/bin/deluge-web -- --port $PORT
  ;;
  status)
    echo "Checking Status of Deluge"
    if (test -f $PIDFileDeluge); then
      PIDVal=$(head -1 $PIDFileDeluge)
      if [ -e /proc/$PIDVal ]; then
        echo -n "DelugeD Running on Process ID: "
        echo $PIDVal
        echo
        ps -H $PIDVal
      else
        echo "DelugeD Not Running but PID File Exists"
        exit 1
      fi
    else
      echo "No PID File found for DelugeD"      
    fi
  ;;
  *)
     echo "Usage: deluge {start|stop|restart|force-reload|status}" >&2
     exit 1
  ;;
esac

exit 0
edit: Yep, MensaWater, deluge is linked to all run levels I have on the machine...

Last edited by monkeyslut; 09-20-2014 at 07:31 AM.
 
Old 09-22-2014, 10:31 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
Maybe issue is that the automatic startup left a lock or pid file behind so your checker is failing when it encounters one of those?

What happens if you change your checker script to do "restart" instead of "start"?
 
Old 09-23-2014, 10:13 AM   #9
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
MensaWater, my previous version of the script was "restart", sadly. So no difference.
 
Old 09-23-2014, 11:04 AM   #10
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 54
Have you tried redirecting the std and stderr to a log file to see if there are errors being thrown by this job?
 
Old 09-23-2014, 10:36 PM   #11
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Yankee, nope, and I'm too noob to know how to. Could you show me example code lines?
 
Old 09-24-2014, 08:09 AM   #12
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 54
/path/to/command > /path/to/log/file 2>&1

the first > redirects stdout to the file. 2>&1 redirects stderr to stdout so that they both end up in the file.
 
1 members found this post helpful.
Old 09-24-2014, 09:43 AM   #13
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks! I'll apply the code later tonight, will get back to you guys then
 
Old 09-24-2014, 09:21 PM   #14
DamnitBoy
LQ Newbie
 
Registered: Sep 2014
Posts: 4

Rep: Reputation: Disabled
Hey monkey, I've very new to Linux, so take this with a grain of salt, but I recently had a similar issue. I created a cron job that points to a script that stops a process, changes it's config file, and then restarts the process and had similar issues. It would work fine when I ran the script by myself through the shell just calling the file ./nigh.sh for example. However, when the cron job would run, the process never got killed and restarted. Come to find out, I had to add sudo to the pkill command and in front of the process when I wanted it to restart. Again, this would run fine when I did it from the command line, but when the cron job attempted to do it, it wouldn't happen. This is because it needed whatever user was going to kill and restart the process to log in with sudo credentials.

I tried a bunch of things I found online like editing visudo that gave the user involved the rights to the pkill process and the start process command so that it wouldn't prompt for a password. (I don't know how safe/secure this is, but it's just a sandbox VM for me to play in)

Again, I'm new to Linux, but I had a similiar issue and this is what fixed it.

I can post my stuff if you think this may be the issue.
 
Old 09-25-2014, 11:52 AM   #15
monkeyslut
LQ Newbie
 
Registered: Aug 2014
Location: malmö
Distribution: Ubuntu x86-64, Raspbian, raspbmc, debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Hey boy

Sudo might solve it, but that would make the Deluge daemon run as superuser. I really don't want to do that, especially since there's a webview window that is accessible from the greater internet. I'm not sure how much damage can be done through that web client, but I don't want to find out; not it is running as superuser.
I might be paranoid, but I like being paranoid when it comes to root access.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to slow down the mouse in raspbian alfiesty Linux - General 3 09-12-2014 07:53 PM
LXer: Raspbian explained LXer Syndicated Linux News 0 08-18-2014 07:20 PM
raspbian/scratch/ssh -X timl Linux - Software 2 06-27-2013 11:14 PM
eth1 failing on boot, IEEE firewire card driver failing, help jackuss_169 Linux - Laptop and Netbook 5 03-05-2005 07:34 AM
LILO install failing, Boot failing, but Installation fine. sramelyk Slackware 9 08-23-2003 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration