LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script not running through cronjob? (https://www.linuxquestions.org/questions/linux-newbie-8/script-not-running-through-cronjob-4175467883/)

NotAComputerGuy 06-30-2013 03:55 AM

Script not running through cronjob?
 
What I'm trying to achieve is for cron to run a command every half hour, which runs a script located in /home/user/.scripts/ creatively called script. This script, as per my previous thread checks if a program is running, if it isn't, runs it.

In /home/user/.scripts/script ls -l shows
Code:

-rwxr-xr-x 1 user user  85 Jun 29 19:40 script
To me this says that it is indeed executable. I can run it using either sh script or ./script, not sure what the difference is.

crontab -l shows:
Code:

*/30 * * * * sh /home/user/.scripts/script
Which to me says that it should be run on every hour and half hour.

And sudo cat /var/log/syslog shows:
Code:

Jun 30 08:00:01 desktop /USR/SBIN/CRON[7660]: (user) CMD (sh /home/user/.scripts/script)
Jun 30 08:30:01 desktop /USR/SBIN/CRON[7676]: (user) CMD (sh /home/user/.scripts/script)
Jun 30 09:00:01 desktop /USR/SBIN/CRON[7745]: (user) CMD (sh /home/user/.scripts/script)

etc. which shows that cron is executing the commands. But it's not actually running when I check.

The script itself works fine if I run it from a command line. I can see that the cronjob is being run. But it isn't working. How do I get insight into what is occurring?

druuna 06-30-2013 04:02 AM

Can you post the script that is being run?

NotAComputerGuy 06-30-2013 04:03 AM

Of course!

Code:

#!/bin/bash
pkill -0 -f "deluge" >& /dev/null
if [[ $? -eq 0 ]]; then
    deluge
fi


druuna 06-30-2013 04:27 AM

That piece of code doesn't look correct. Try this instead:
Code:

#!/bin/bash
pgrep -f "deluge" 2>&1>/dev/null
if [[ "$?" -ne "0" ]]
then
  deluge &
fi

I don't know what deluge does, and you might want to include the full path to the executable.

jpollard 06-30-2013 04:42 AM

Two items -
1. you assume that "deluge" and "pkill" (or pgrep) is in the search path for the program. It won't work if they aren't.

2. (this is a bit of a nit and can be ignored) you assume that the process named "deluge" is actually the one you want... A process can have any name it wants - If you make a symbolic link to "ls" and name it deluge, then, when run, ls will be identified as "deluge".

Have you checked your local mail? cron will mail any output from a job to the local mail account, so any errors generated by the script will be mailed to you.

One more thing - does "deluge" require a GUI? If it does, then it will not necessarily be available - you might be logged out, and if you log in it may not be able to get the correct access key as it would have been cut off when you logged out...

NotAComputerGuy 06-30-2013 05:37 AM

Quote:

Originally Posted by druuna (Post 4981096)
That piece of code doesn't look correct. Try this instead:
Code:

#!/bin/bash
pgrep -f "deluge" 2>&1>/dev/null
if [[ "$?" -ne "0" ]]
then
  deluge &
fi

I don't know what deluge does, and you might want to include the full path to the executable.

Hi,

Thanks for the idea. Unfortunately the script doesn't work and I don't know what you mean by 'the full path to the executable'. I type in deluge in the command line and it runs.

jpollard, I'm afraid I don't really understand what you've said. I didn't know I had local mail either. It does have a GUI, but the computer is always logged in.

I just want my computer to start deluge at boot, which sometimes it does, other times it doesn't. This is why I thought this script which runs every half hour would fix that. Check it's running, if not, start it.

NotAComputerGuy 06-30-2013 05:56 AM

Just a random idea I've just had, but can't find how to do it at the moment. If I set the script to start on boot, the I could have the script loop itself infinitely every 30 minutes.

Code:

#!/bin/bash
while :
do

pkill -0 -f "deluge" >& /dev/null
if [[ $? -eq 0 ]]; then
    deluge &
fi

sleep 30m
done

Does the while, do, sleep and done elements of this script work? I appreciate that people think the script could be better, but I just want a very simple script that is basic, quick and works. I hope I never have to do any other scripting ever again, so I'm not too fussed if it's a bit ugly. :)

Thanks

druuna 06-30-2013 06:09 AM

Quote:

Originally Posted by NotAComputerGuy (Post 4981120)
I just want my computer to start deluge at boot, which sometimes it does, other times it doesn't. This is why I thought this script which runs every half hour would fix that. Check it's running, if not, start it.

I think your approach to the actual problem (deluge starting/not starting at boot) is wrong.

You need to find out why deluge behaves this way. Putting a script in place that starts deluge if it isn't running makes the problem harder to trouble-shoot: Is deluge not starting or is the script faulty or possibly both.

Once you figure out why deluge behaves the way it does during boot you can tackle that and your problem is solved.

If deluge does start a gui: You cannot use cron to accomplish this (as already stated by jpollard) and I doubt it will start during the boot process.

EDIT: As a workaround you might use the ability of Desktop Managers (gnome/kde/etc) to start a program when you log in.

NotAComputerGuy 06-30-2013 03:25 PM

How do I tell why MATE appears to have forgotten to start Deluge?

Quote:

Originally Posted by druuna (Post 4981134)
EDIT: As a workaround you might use the ability of Desktop Managers (gnome/kde/etc) to start a program when you log in.

This is what is used to start the program. 3 times out of four, Deluge starts with no problem. Occasionally it forgets to start deluge.

jpollard 06-30-2013 08:21 PM

Quote:

Originally Posted by NotAComputerGuy (Post 4981312)
How do I tell why MATE appears to have forgotten to start Deluge?



This is what is used to start the program. 3 times out of four, Deluge starts with no problem. Occasionally it forgets to start deluge.

Then you have a different problem. If deluge requires a GUI to work, it will almost never work through a cron job as it does not have access to the GUI.

NotAComputerGuy 07-05-2013 02:46 PM

I tried adding the script to /etc/init.d/ which didn't work, and then I added it to /etc/network/if-up.d/ and now my computer won't boot. If I manage to fix this, please can someone just tell me how I can get the computer to run this script at startup in a how to style? I'm quite sick of guessing my way through it and to be honest it was inevitable that eventually I was going to break the computer.

Thanks

druuna 07-06-2013 02:33 AM

I just had a look at the deluge web pages and noticed that this bittorrent-client has 2 parts: A daemon (deluged) and a user interface (deluge).

The part you are talking about is the user interface, which is a graphical interface. As stated before by jpollard and me: You cannot start this at boot or by using cron.

In post #6 you tell us that deluge sometimes works and sometimes it doesn't. I don't think a script will solve this problem, you'll need to figure out why deluge (and/or deluged) acts this way. I suspect that something is wrong with the setup and that is why you are having problems.

I cannot point you to anything specific (I have no experience whatsoever with bittorrent clients in general and deluge in specific). Do have a look at the documentation that is present on the deluge web site and maybe the forum on that site might be able to help you.

NotAComputerGuy 07-09-2013 04:10 AM

I think maybe we are using the same words to describe different things. The computer that this is running on automatically logs in for me, and therefore I want this to run once the computer is booted and therefore logged on.

If I press the power button on my computer, after it does it's thing, with no input from anyone, it will wait on the desktop, logged in ready for me to go. I open a terminal and type:
Code:

sh /home/user/.scripts/script
and the script runs and works fine. This is what I want the computer to do. Wait for it to log itself in, and then run that script.

Is this still an impossibility even with this further explanation?

druuna 07-09-2013 04:16 AM

As I mentioned earlier:
Quote:

Originally Posted by druuna (Post 4981134)
EDIT: As a workaround you might use the ability of Desktop Managers (gnome/kde/etc) to start a program when you log in.

If that doesn't (always) work: Have a look at the log files. It is not clear why it sometimes doesn't work, so I cannot point you to a specific log file. I would start with the deluge(d) log files.

Madhu Desai 07-09-2013 05:15 AM

Quote:

Originally Posted by NotAComputerGuy (Post 4981120)
I don't know what you mean by 'the full path to the executable'. I type in deluge in the command line and it runs.

To get full path of app
Code:

$ which deluge
/usr/bin/deluge

Quote:

Originally Posted by NotAComputerGuy (Post 4981120)
I'm afraid I don't really understand what you've said. I didn't know I had local mail either.

To check for mail, just enter 'mail'
Code:

$ mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/madhu": 1 message 1 new
>N  1 root                  Tue Jul  9 15:40  18/643  "Did you check bootlog?"
&


Quote:

Originally Posted by NotAComputerGuy (Post 4981120)
I just want my computer to start deluge at boot, which sometimes it does, other times it doesn't. This is why I thought this script which runs every half hour would fix that. Check it's running, if not, start it.

If you are using GUI, then why dont you start program at startup in System -> Preferences -> Startup Application


All times are GMT -5. The time now is 07:04 PM.