I'm new to cron jobs. I read a post on how to write a cron job with crontab.
So my crontab looks like this:
1 * * * * /Users/apple/Desktop/wget/down.sh
which basically means that every minute i want to execute the script :down.sh. Now the script runs
fine manually. The script is a simple program that downloads a PDF from the internet:
#!/bin/bash
wget -U Mozilla -t 1 -nd -A pdf "http://www.fi.usj.edu.lb/images/stories/HoraireS08/3eli.pdf" -e robots=off;
I don't know why it's not running every minute once the terminal tells me that he's installing the new crontab.
Can somebody help me please?
Solution:
Thank you all for your help, the syntax as mcalex said should be
* */1 * * * path/to/script
if you want it to be executed every hour.
The cron job was working normally.However my mistake was simply writing permissions, in fact while executing the wget command, it's supposed to write the pdf file in the current workind directory which is a system directory in case of the cron tab. so i solved my problem simply by navigating to the Desktop directory before executing the wget command like so:
cd /Users/apple/Desktop/wget
and then do whatever i want to do.
PS: i should include the full path of the wget command too.
Thank you all for you help again:)
When you put 1 in the first column, it will run on the first minute (of every hour). In order to get it to run in every minute of every hour, you need to set the minute column as */1
So your line should read:
*/1 * * * * /Users/apple/Desktop/wget/down.sh
supporting links:
job every minute: https://bbs.archlinux.org/viewtopic.php?id=59180
job every 5 minutes: http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/
1 * * * * /Users/apple/Destop/wget/down.sh
From this entry script will never run on every minute because it will run on first minute of every hour.
Make this change to your crontab file to run this script every min.
"* * * * * /Users/apple/Destop/wget/down.sh"
Do you have a typo? It looks like you might have mis-typed Desktop?
Another thing to do is to redirect the output of running the script to a file so you can see what's going on like this:
1 * * * * /Users/apple/Destop/wget/down.sh >> /tmp/cron.out
and then check out the file to see what's going on.
Did cron send you some email detailing what went wrong?
Does the script under that path exist?
Note that cron uses /bin/sh to execute commands.
Did you set a proper PATH in your script? wget may not be in the default PATH or there may be no PATH at all. Try using /path/to/wget in the script.
Note that downloading the same PDF file once a minute is probably a silly idea, though...
If your cronjob is writing things to disk then be careful that system preference "Put hard disk to sleep when possible" is unchecked.
This was preventing my cronjob backup tasks to execute.
Related
Here's the content of my crontab file:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="example#example.com"
*/5 * * * * sh /robot/1/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/2/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/3/master.sh
*/5 * * * * sh /robot/4/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/5/master.sh >/dev/null 2>&1
This is the error that keeps showing in /var/log/cron when it tries to run:
crond[669]: (sh) ERROR (getpwnam() failed)
If I run any of these files manually, they work without any issues.
What's wrong with the crontab file?
It surprises me that nobody has the correct answer to this. Today i faced exactly the same problem and google didn't help.
After 2 hours i found that when placing a file in /etc/cron.d the schedule line has to contain an extra option.....
I allways use this for my crontab -e
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) /my/fancy/script.sh
So it contains 6 items.
When placing this in a file inside /etc/cron.d the cron needs an extra option, being the user to run your fancy/script.
# Minute Hour Day of Month Month Day of Week Who Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) root /my/fancy/script.sh
This is documented in man crontab(5). For example https://linux.die.net/man/5/crontab . It says:
Jobs in /etc/cron.d/
The jobs in cron.d are system jobs, which are used usually for more than one user. That's the reason why is name of the user needed. MAILTO on the first line is optional.
The sixth position is reserved for username running the job. You specified a user called sh which is most probably not present on the machine.
simple answer
on your crontab you need to specify the USER to run the command
example to run as ROOT is:-
0,10,20,30,40,50 * * * * root /path_to_script/script_name
or to run as user FRED
0,10,20,30,40,50 * * * * fred /path_to_script/script_name
default with no USER specified is to run as user CRON and that user would not have permissions to execute the script
We can create cron jobs for system as well for individuals. The crontab in /etc/crontab specifically used for system cronjobs. So you need to specify the cronjob command executed by whom. In the question the username not specified. Hence the ERROR (getpwnam() failed) occurs. You can create user specific cronjobs in /var/spool/cron/username
NOTE:: Cron jobs are very useful but disastrous on failures!
Nothing is wrong with the crontab file (so long as by "my" crontab, you mean that it's a user crontab rather than a system crontab; otherwise, see other answer).
On the other hand, something is wrong with your system's directory service -- as configured, in Linux, with nsswitch.conf. Perhaps you're using a Kerberos-authenticated LDAP store, and your cron daemon doesn't have a Kerberos token to connect to it (or is sandboxed, as with SELinux, not to have network access); perhaps it's a file store that isn't readable by the user whose crontab is being run; perhaps some other odd and interesting thing is going on.
getpwnam() is a C library call that performs a lookup for the name of the currently-logged-in user. If your shell were bash, it would fall back to a name of I have no name! -- so this error means your sh implementation is something different. (If you want to run your scripts with bash, use bash, not sh).
I have gone through all sorts of answers and replies up and down the Internet and nothing to seem to work for me. I want to simply run a bash script every minute using CRONTAB on Raspberry PI on Raspbian.
I have a script called autocon.sh and I simply entered into crontab as follows:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
...
1 * * * * root bash /home/pi/autocon.sh
BUT IT WON'T RUN.
What am I doing wrong?
I'm not sure where the 'root' part comes from, but I'm guessing you're wanting to run the script as root? If so, you need to put an entry into the root crontab, do so by running:
sudo crontab -e
This will open up the root user crontab for editing, anything run from said location will run with root priveleges.
Insert the following line:
* * * * * bash /home/pi/autocon.sh
That should do it :) The 1 in your script actually means 'run at 1 minute past the hour' and thus in your case, 1 minute past every hour - easy mistake! Replacing it with a * means every minute.
The syntax is:
minute - hour - day of month - month - day of week - command
Additionally, if you make your script executable, like so:
sudo chmod +x /home/pi/autocon.sh
You can omit the 'bash' command, and simply use:
* * * * * /home/pi/autocon.sh
And unless you're using the two lines at the top for something in particular, you can omit those too.
For clarity, Barmar's comment on my original post:
In per-user crontab files you don't put the username. But in
/etc/crontab you do.
To run a cronjob every minute, all the values must be stars. Your cronjob is set to run at 1 minute past the hour every hour.
It should be:
* * * * * root bash /home/pi/autocon.sh
I've been working on a project and I would so not like it to be taken by system crash.
So I wrote a script to backup my whole project directory into Dropbox.
But I had to run it every 10 min, if I could remember to do that.
Question: any way to auto-it-up?
Type the following command to enter cronjob:
$ crontab -e
To get crontab to run a task every 10 minutes you could type as follow:
*/10 * * * * /path_to_script
See additional read for it:
Wikipedia
cron-every-5-minutes
cron job every 5 minutes starting from a specific time
try this solution:
cat cronjob
*/10 * * * * sh /path/to/scipt.sh
Then:
chmod +x /path/to/scipt.sh
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
The obvious solution is by crontab, as others suggest. Here is another solution, by adding only one line to your backup-script.
If your system has installed and enabled the "at" suite, see:
man atd
man at
or atrun if you using bsd like system
you can simply queue the "next run-time" from your backup script. So, for example, if your backup script is called /home/joe/bin/copy_to_dropbox, add to the end of script the next line:
af -f /home/joe/bin/copy_to_dropbox now +10 minute
and run manually the backup script first time.
After the first backup, the at command in the script queues itself for the next execution.
You can check the at queue with the "atq" command.
I want to use Dropbox to keep track of my computer's IP address.
I was thinking of using crond to run a shell script every 5 minutes that asks icanhazip.com what my ip address is and then edit a file to have the current ip of that computer.
So i think the script would go something like this
CurrentIP = curl -s http://icanhazip.com/
echo $CurrentIP > ~/Dropbox/ComputerIP
then I would add an entry to crontab that would tell it to run every ten minutes
*/5 * * * * UpdateIP.sh
Mind you, this is my first shell script, so I have no idea what I´m doing, and would appreciate any input into how to do this more elegantly
The simplest would be to run your command directly from cron:
*/5 * * * * /usr/bin/curl -s http://icanhazip.com/ > ~/Dropbox/ComputerIP
(include the correct full-path to your curl command).
Of course, if you expect that you'd need to perform a more complicated task in the future, it is better to leave it in a script, as you already do.
I have written a cron job as follows:
$crontab -e
Then it opened a file where i wrote
5 * * * * USERNAME PATH-TO-SHELLSCRIPT
How would i come to know that my job has been executed?
Do something with the script? Edit a file and add timestamp for example to see if its ok :)
You can use a log file. Create a file under your home directory, e.g., "/home/user/.cronlog/script1", and output script execution time and status messages with timestamps. You can then check the logs to see script history. To test your script first execute it every minute. Then change the crontab entry to the actual period.
Also make sure you understand the time parameters so you know when to expect it to work :)
I use crontab for a 5 min interval cron like this: */5 * * * *
http://en.wikipedia.org/wiki/Cron