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
Mark Roberts pointed out a few things I'd done wrong.
Namely, the spaces here
Get rid of those spaces..
Next, having Cron configuration fixed to email every minute.. instead of what I had :
Seems to me Lubuntu doesn't support the @Reboot Cron syntax.
From what I've discovered just now, the
@reboot
syntax seems to depend on whatcrontab
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:
I managed to get @reboot working with the answer @halfer provided, but want to add an interesting abstract from
man 5 crontab
So you might check these as well in case the job is still not running.
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.