Reputation: 35
I wrote a very simple script to copy files from one dir into another one, I want to create a cronjob - by the way, I don't have "crontab" here but "fcrontab" instead since scheduler isn't cron but fcron - so it runs once a week - on Sundays - but I'm not sure whether if it's correct or not. Can you take a look and tell me so?
this is the cronjob:
0 1 * 1-12 SUN /home/myusername/dir/cp.sh
or
0 1 * 1-12 SUN /bin/bash /home/myusername/dir/cp.sh
I've read quite a few posts from here as well as over the web but I'm still confused. Thanks in advance for your answers.
Upvotes: 1
Views: 361
Reputation: 189327
The weekday field contains a number where 0 represents Sunday. There is no support for human-readable weekdays in any crontab
variant I have come across.
If you want this to execute every month, just put *
for the month.
0 1 * * 0 /home/myusername/dir/cp.sh
As long as the script is executable (chmod +x cp.sh
) and has a valid shebang (#!/bin/bash
as the very first line of the file) you don't need to explicitly tell the OS to run it with bash
, just like on the command line.
crontab
runs your jobs from your home directory so you could replace /home/myusername
with .
if this is running from myusername
's account. If $HOME/dir
is in your PATH
you only need cp.sh
(but take care to set the PATH
where cron
, too, can see it!)
Any output or error messages will be sent by email if your server is set up to handle that. This is slightly obscure and sometimes bewildering, especially if you don't actually have email properly configured, so many users add a redirection to a log file for every cron job.
0 1 * * 0 cp.sh >>cp.sh.log 2>&1
(Some beginners like to redirect everything to /dev/null
and then come here to ask us what's wrong when there's an error. Of course, we don't know, either.)
The Stack Overflow crontab
tag info page has syntax advice, troubleshooting tips, and a link to a site where you can generate a valid crontab
from a newbie-friendly form where you just click buttons and move sliders to say when your job should run.
Upvotes: 1