1

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.

Moltke
  • 35
  • 3
  • did you see this [answer](https://stackoverflow.com/a/878647/1425697) by @dogbane ? – AnderCover Sep 19 '18 at 00:35
  • 1
    Possible duplicate of [How to create a cron job using Bash automatically without the interactive editor?](https://stackoverflow.com/questions/878600/how-to-create-a-cron-job-using-bash-automatically-without-the-interactive-editor) – AnderCover Sep 19 '18 at 00:36
  • 1
    @AnderCover Yes, I did see that post and I forgot to mention - sorry about that - but I don't have "crontab" but "fcrontab" instead and that what confused the most but after some googling and reading about it now I'm good. It's basically the same. According to some is a "cron-improved" but I wouldn't know about that. Thank you for your answer. http://fcron.free.fr/ – Moltke Sep 20 '18 at 19:39

1 Answers1

1

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.

tripleee
  • 139,311
  • 24
  • 207
  • 268
  • Thank you! Well, according to this site https://crontab.guru/ syntax's ok. I used it to create the task, and I did wildcard month just like you suggested. By the way, I forgot to mention - sorry about that - but I don't have "crontab" but "fcrontab" instead and that what confused me the most, but now I'm good. – – Moltke Sep 20 '18 at 19:46
  • Whaddyaknow, weekday names are actually supported on both BSD and Linux. TIL! – tripleee Sep 21 '18 at 06:17