0

Problem

I have a script to be run at 1:32 am, so I set a cronjob by

$ crontab -e

And in the editting file, I have

32 1 * * * /home/user/.scripts/midnightjobs

where "user" is my user name. However, it did not work.

Attempts I made

I tried adding a logging function in my script, and hoped to see what was wrong. It seems like the script never has run.

I also tried adding another cronjob at 7:59am:

0 8 * * * /home/user/.scripts/midnightjobs

And it works! The script ran, and did output a log file at 8 am.

My guess

I believe I have been very careful.. and based on my second attempt, my best guess is that my laptop (running on an archlinux) secretly falls asleep at nights, failing to run the cronjob.

  • 5
    Are you able to investigate cron's log to see whether it ran the job or not at 01:32? – Kusalananda May 3 at 12:43
  • You can verify your guess to know that if your system was up&running at 1AM by referring to this Q&A or not. – αғsнιη May 3 at 15:24
  • @Kusalananda I could not find my cron's log.. but my script should auto-log if executed. – Student May 3 at 19:45
  • @αғsнιη I tried. My laptop did not fall asleep. – Student May 3 at 19:46
  • hmm, does your script has any sleep or some dependency that needs wait for something or check the time itself within itself? is your 1:32AM task last line in your crontab? is there an empty line at the end of your crontab? – αғsнιη May 3 at 20:01
0

A few suggestions:

  1. Take a look at your cron logs (may be in /var/log/syslog or /var/log/messages) to verify the script is being run.

  2. Make sure your script is executable

  3. Make sure your script runs correctly when run manually

  4. Bare in mind that you have a different environment when running a script via cron, so things like your path may be different causing errors in your script

  5. Add > /tmp/cron.output.log to the end of the crontab line to write the script output to a file you can look at. If the file exists then the script ran.

  • Thank you for your answer. I tried the first solution, but I have no files like that under /var/log. – Student May 3 at 19:40
  • For 2~4, I don't think that's a problem, based on my second attempt. – Student May 3 at 19:40
  • For 5, I tested by adding " 40 15 * * * /home/user/.scripts/midnightjobs > /home/user/cron.log ", but nothing happened at 15:40. – Student May 3 at 19:42
  • (contd for 5) I also tried my second attempt again by adding " 41 15 * * * /home/user/.scripts/midnightjobs ", and it worked again! – Student May 3 at 19:42
  • Do any cronjobs work? Try just adding a simple cronjob echoing something to a file or something. Make sure you have a cron daemon enabled and running. – rusty shackleford May 7 at 14:37
0

If you don't put any : MAILTO="" in the head of crontab, you should have (if your PC was up) a mail for the user @ each run of the script.

  • Do you mean "if I put any..."? Sorry, I don't think I understand what you mean. – Student May 3 at 19:43
  • With MAILTO="" at the top of the crontab the mails are sent to /dev/null and if you don't have it the mails are sent to the user launching the job. – admstg May 6 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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