Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script that reminds me to restart my computer if uptime is more than, say 3 days (although its set to 0 days now just to check if the script is running as my computer has been up only over a day..).

I realize it isn't the most elegant script but I am trying! :)

#!/bin/bash

up=$(uptime | grep "day" > /home/dnaneet/uptime.foo && awk < /home/dnaneet/uptime.foo '{ print $3 }')

[[ $up -gt 0 ]] && xmessage -center "Restart!"

I have made it executable by chmod + x checkup.sh and it works fine when I run it fro the terminal via ./checkup.sh

My crontab entry for this script is:

46 14 * * * /home/dnaneet/Desktop/./checkup.sh

So it runs at 14:46hrs daily...

So... I am thinking it should run, unless I didn't something really silly. Also, do you think it's ok to move this bash script to /bin?

share|improve this question
1  
I would put it in /opt/bin or ~/bin as a general best practice to not muck up where packages think things are. –  hbdgaf Apr 1 '12 at 18:55
    
If you run the script like so: /home/dnaneet/Desktop/./checkup.sh does it work? I would put /home/dnaneet/Desktop/checkup.sh –  LnxSlck Apr 1 '12 at 18:59
    
@LnxSlck Yes, running /home/dnaneet/Desktop/./checkup.sh works fine. But /home/dnaneet/Desktop/checkup.sh may not work right? Because it needs a ./? –  drN Apr 1 '12 at 19:03
1  
I don't think cron knows anything about your X session. –  glenn jackman Apr 1 '12 at 19:34
    
@glennjackman So what does that mean? Does it mean that running such a script through crontab doesn't allow a pop up message to show up in the gnome environment? –  drN Apr 1 '12 at 19:38

2 Answers 2

up vote 17 down vote accepted

One thing at a time:

First let's give you a user based bin folder: cd ~/ && mkdir bin

You want to use crontab. Let's start with something really simple:
* * * * * touch /tmp/testing.txt
Okay, so that works

Now let's try running a script that does the same
* * * * * /home/username/bin/touchtest.sh to run once a minute until you get it working
No you don't need a ./ in the middle of the line. ./ is for when you are giving relative urls.
Okay, so that works

Now let's try running a script that calls xmessage
* * * * * /home/username/bin/rebootwarn.sh
not working

First we need to not depend on environment variables. This includes path setting, x11 settings, or anything else(python and ruby environment variables come to mind...)

Let's make ours look a bit like anacron's proper cron file..I saved this as test

#Borrowed from anacron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#End borrowed from anacron

* * * * *   /bin/bash /home/username/bin/test.sh

Set to run once a minute

crontab test to import it

On to the script

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
up=$(uptime | grep "day" > /home/dnaneet/uptime.foo && awk < /home/dnaneet/uptime.foo '{ print $3 }')

[[ $up -gt 0 ]] && xmessage -center "Restart!"`

Okay, so that works...what did we do?
We changed all the commands not to depend on paths we didn't explicitly set
We ran our script explicitly with bash
We told the script that we expect to be on DISPLAY :0.0

share|improve this answer
    
Thanks for the answer. You said "Let's make ours look a bit like anacron's proper cron file..I saved this as test".. OUR what? The crontab file? :-/ –  drN Apr 1 '12 at 20:23
    
Heres what I observed. First I had to get rid of the space before and after = for the line specifying DISPLAY. Second, when I edited my crontab file and added the PATH as suggested and set the time to run at a later time, the pop up window didn't show. However, the popup window showed fine when running the script using /bin/bash... what gives? –  drN Apr 1 '12 at 20:34
    
What do you know, once I changed my script to export DISPLAY=:0.0, it worked fine. Although there was a significant lang of about 5 seconds after the clock chimed the hour when my pop up window should have showed... Any suggestions? But yes, your amends to the script work! –  drN Apr 1 '12 at 20:49
    
No idea on the 5 second lag. –  hbdgaf Apr 1 '12 at 20:51

The problem was solved read the manual in Google:

description:
1. I have script using #!/bin/bash as header
2. I put the script on /home/wc3/palert/

analysis:
1. crontab can't run my script
2. But if I run manually it show the output and also I can see the result on my web
3. It means crontab can't get the environment not like when you run your script on your script folder

answer: 1. put your environment using this line below on your script with:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/wc3/palert

change this: /home/wc3/palert
with: the place that you put your script e.g. /home/budi/script.sh

sample:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/budi
# Script continues...

Try put your script on crontab .....

share|improve this answer

protected by Braiam Feb 7 at 4:39

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.