Results 1 to 7 of 7
Hi,
I was wondering if I can get some help with trying to run a script via crontab on Ubuntu Server 10.04.
In the script called simplescript.sh, I have the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-15-2011 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 9
issue getting a script to run via crontab
Hi,
I was wondering if I can get some help with trying to run a script via crontab on Ubuntu Server 10.04.
In the script called simplescript.sh, I have the following:
#!/bin/bash
sudo cp ~/Documents/hello_world hello_world1
and that's it. a really simple script.
In the crontab, I have written as follows
00 13 * * * root /home/user1/Documents/simplescript.sh
and then saved it.
After 1pm, I went to check to see if it worked and I found it did not. I did not see hello_world1 file being copied.
Could anyone tell me where I did it wrong.
Thank you so much
- 11-15-2011 #2Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 398
Since you have a username inthe crontab, you must be using the system crontab rather than your own user's crontab.
If you at using your own user's crontab, remove "root" as all jobs will run as you.
Have you changed the permissions on your script to be executable?
- 11-15-2011 #3
The way you wrote it, you don't need sudo. You told the script to run as root.
If you invoke sudo though, you will need to ensure that you can run the script with sudo WITHOUT a password. Also, you will need to comment out "requiretty" in your sudoers file. Otherwise it will not allow you to invoke sudo.linux user # 503963
- 11-15-2011 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 9
Thanks alf55 for your reply.
When I run $ls -l at ~/Documents, I got it as follows
rwxr-xr-x 1 root root 91 2011-11-15 ... simplescript.sh
So it's been given 755 permission.
Then, I removed the root in the crontab as shown below
30 09 * * * /home/user1/Documents/simplescript.sh
This did not work either. I run out of clues as to why it does not run.
Another question I'd like to ask is whether or not I need to out a ./ in front of simplescript.sh in the crontab. I am not sure if crontab recognises this is a script that it needs to execute.
Thank you
- 11-15-2011 #5
If you are using crontab, give a username you are going to invoke the script as:
Use of ./ will not work. As cron's present working directory will not be where your script is located. So give it the full path. The script is executable. If its not doing anything, something is wrong with your script then.Code:0 13 * * * someuser /path/to/script
Check messages, or cron log to ensure it is indeed firing off the cronjob.
And if you are using sudo, read my first reply.linux user # 503963
- 11-16-2011 #6Just Joined!
- Join Date
- Mar 2010
- Posts
- 9
Thank you scathefire for your replies. Sorry I missed your first reply. For some unknown reason, I did not get to see your reply when refreshing my firefox browser.
Anyway, it works now. Thanks for your tips and help. The problem was in the script, I did not make it clear with the path to the script.
before:
sudo cp ~/Documents/hello_world hello_world1
After:
cp /home/user1/Documents/hello_world /home/user1/Documents/hello_world1
Apparently it doesn't recognize the ~ character. Do you happen to know why.
Anyway, it worked like a charm after the amendments. Thanks once again.
- 11-16-2011 #7
If I had to take a guess, it would probably have something to do with environment variables like that '~' are not properly passed inside of cron.
But that's just my guess.linux user # 503963


Reply With Quote
