2

I have a Python script which simply writes some text and saves it to a file

#! /usr/bin/python3
def main():
     filename = '/home/user/testHello.txt'
     openfile = open(filename,"w")
     print("Hello CRON", file = openfile)
if __name__ == "__main__":
     main();

I want to execute this script at startup via CRON. So I edit the crontab listing by using

>crontab -e

My entire crontab looks like :

SHELL = /bin/bash
PATH = /sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME = /
# run-parts
1 * * * * /home/user/tester.py
@reboot /home/user/tester.py

This is the location of the file, and the file has permissions to execute. I can run the file no problem as a script from the commandline. Yet when I restart the machine, no file is generated. I am trying to understand why, and played around with the crontab entry.

@reboot /usr/bin/python3 /home/user/tester.py

This didn't work either.

Edit:

ps aux | grep crond 

gives me

user     2259 0.0 0.0.  9436  948 pts/0 S+ 23:39   0:00 grep --color=auto crond

I am unsure how to check if crond is running, or if the user in question is mounted before/after CRON. I'll try with:

sudo crontab -e 

but that hasn't worked either.

Running:

pgrep cron

returns 957

halfer
  • 19,824
  • 17
  • 99
  • 186
Erik
  • 2,782
  • 3
  • 34
  • 64
  • 3
    1) check if crond running at boot? and 2) check to see if /home/user is mounted at time when crond executes 3) your version of crond may not support @reboot are you using vix's crond? ... show results of crontab -l -u user – Ahmed Masud Aug 10 '13 at 03:43
  • @inquisitor What? Nobody wants to trigger a reboot. It is about executing a command AFTER a reboot... – glglgl Aug 10 '13 at 05:23
  • Borrowing from this page : http://www.cyberciti.biz/faq/howto-check-cronjob-is-running-not/ It appears crond or its logs aren't active.. Be advised I'm using Lubuntu and chkconfig is not loaded. – Erik Aug 10 '13 at 06:44
  • It might be a good idea to set it up as an init script instead of relying on a specific version of cron's @reboot. – Mark Roberts Aug 10 '13 at 06:44
  • @MarkRoberts , I also tried just having it run every 5 minutes .. still nothing happening. – Erik Aug 10 '13 at 06:45
  • running >pgrep cron returns 957 (The PID I'm guessing) – Erik Aug 10 '13 at 06:47
  • Erik, I was suggesting not to use cron at all. One thing you may want to do is setup MAILTO so that you can see if the cron is failing due to permissions or something. – Mark Roberts Aug 10 '13 at 06:48
  • @MarkRoberts I see, perhaps you're right but I wanted to learn CRON and it seems this very basic thing is beyond me. If I get stuck I'll move into just an init script. I've also setup the MAILTO , but I'm very new and not seeing any output yet. Wondering if it's configured properly.. – Erik Aug 10 '13 at 06:50
  • Who is the crontab running under? Can you execute the script as that person in their home directory? – Mark Roberts Aug 10 '13 at 06:51
  • @MarkRoberts this is a fresh virtual environment, I can run the program as that user , root, etc.. it works at the command line just fine as 'user' .. just not working on their crontab. – Erik Aug 10 '13 at 06:54
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35185/discussion-between-mark-roberts-and-erik) – Mark Roberts Aug 10 '13 at 06:56
  • @MarkRoberts removed the reboot and modified the 1 * * * * , to */1 * * * * , problem is solved! Where do I send the rep pts Mark? Thank you! – Erik Aug 10 '13 at 07:17

4 Answers4

5

From what I've discovered just now, the @reboot syntax seems to depend on what crontab you're editing. I found that for the system-level /etc/cron.d/ folder, entries there must have a user, just like regular time-based crons.

Thus this worked for me, on Ubuntu 14.04, to run the specified command as root on startup:

@reboot root /home/vagrant/log.sh
halfer
  • 19,824
  • 17
  • 99
  • 186
3

Mark Roberts pointed out a few things I'd done wrong.

Namely, the spaces here

MAIL = root
HOME = /

Get rid of those spaces..

Next, having Cron configuration fixed to email every minute.. instead of what I had :

*/1 * * * * /home/user/tester.py

Seems to me Lubuntu doesn't support the @Reboot Cron syntax.

Erik
  • 2,782
  • 3
  • 34
  • 64
2

I've had a similar problem with a @reboot cron job not running; in case it helps anyone else:

The problem for me is that my home directory is encrypted with eCryptfs (which is what you get if you choose to encrypt your home directory when installing Ubuntu) - broadly speaking this means that the contents of your home directory aren't available until you log in, but cron runs @reboot jobs on reboot, not when you log in.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • How do you know if your home is encrypted? lets say if you were not the one that setup the server. I still cant get @reboot to work on my ubuntu 16 – Oluwatumbi Jun 28 '18 at 04:30
  • 1
    @Oluwatumbi I can tell by typing `mount | grep home` - I see a line beginning: `/home/.ecryptfs/myaccount/.Private on /home/myaccount type ecryptfs` – Mark Longair Jul 01 '18 at 19:33
0

I managed to get @reboot working with the answer @halfer provided, but want to add an interesting abstract from man 5 crontab

... The format of a cron command is very much the V7 standard, with a number of upward-compatible extensions. Each line has five time and date fields, followed by a command, followed by a newline character ('\n'). The system crontab (/etc/crontab) uses the same format, except that the username for the command is specified after the time and date fields and before the command. The fields may be separated by spaces or tabs. The maximum permitted length for the command field is 998 characters. ...

So you might check these as well in case the job is still not running.

dmytro.poliarush
  • 383
  • 4
  • 11