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.

Student
  • 433
  • 6
  • 14
  • 5
    Are you able to investigate `cron`'s log to see whether it ran the job or not at 01:32? – Kusalananda May 03 '19 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](https://unix.stackexchange.com/q/131775/72456) or not. – αғsнιη May 03 '19 at 15:24
  • @Kusalananda I could not find my cron's log.. but my script should auto-log if executed. – Student May 03 '19 at 19:45
  • @αғsнιη I tried. My laptop did not fall asleep. – Student May 03 '19 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 03 '19 at 20:01
  • It does not have any "sleep". @αғsнιη good point! It is in the last line in my crontab, but I cannot be sure if there *was* an empty line at the end! I will make sure there are no empty lines tonight and see if it works tomorrow morning :) – Student May 03 '19 at 22:34
  • @αғsнιη I double-checked last night that there is not empty line at the end of my crontab. But turns out that it still did not work.. how mysterious is this – Student May 04 '19 at 17:41
  • Ok, have a empty line after last cron task you have, and check if it execute your task now, if you dont have that empty line thrn your last task will never run. – αғsнιη May 05 '19 at 04:47
  • Does Arch (specifically your instance of it) use encrypted home folders? If so, are you logged in when it works but logged out when it doesn't? – roaima May 08 '19 at 11:30

2 Answers2

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.

rusty shackleford
  • 2,365
  • 9
  • 21
  • Thank you for your answer. I tried the first solution, but I have no files like that under /var/log. – Student May 03 '19 at 19:40
  • For 2~4, I don't think that's a problem, based on my second attempt. – Student May 03 '19 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 03 '19 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 03 '19 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 07 '19 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.

admstg
  • 322
  • 2
  • 11
  • Do you mean "if I put any..."? Sorry, I don't think I understand what you mean. – Student May 03 '19 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 06 '19 at 08:11