I wrote a Python script which I need it to run every 5 mins. My server is running CentOS 6.4 Final. Here's what I did in detail.
After logging into the server with an account has root access, I did cd /var/spool/cron/, I can see a couple of files has different usernames on it. Edit my file (the one has my username on it) with nano myusername and I added this line at the end of the file.
*/5 * * * * /usr/bin/python /home/myusername/Dev/cron/python_sql_image.py
I waited a bit and the cronjob works now. But new question: this Python code will generate a png file after being executed. When I manually run it, the png file will be created under the same folder with the py script, but when cronjob runs it, the png file was created on /home/myusername. Is there anyway I can change the location?
2 Answers 2
Simply change the working directory:
*/5 * * * * cd /home/myusername/Dev/cron && /usr/bin/python python_sql_image.py
Comments
Each line that contains a job must end in a newline character.
crontab -e? I know some require a newline at the end of your crontab.crontab -eas suggested above :)crontab -eon CentOS is a bit weird. It looks like Vim but doesn't work as Vim does. When I want to quit, hitting Escape didn't work..EDITOR=/usr/bin/nano crontab -e(maybeyum install nanobefore). Nano is far less powerful than vim, but more user-friendly.