0

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?

asked Jan 21, 2014 at 18:35
5
  • why not just use crontab -e ? I know some require a newline at the end of your crontab. Commented Jan 21, 2014 at 18:37
  • Check that the script is executable by the user running the cron daemon and also check if the daemon can read the file. And use crontab -e as suggested above :) Commented Jan 21, 2014 at 18:38
  • @C.B., crontab -e on 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.. Commented Jan 21, 2014 at 18:39
  • Then try with EDITOR=/usr/bin/nano crontab -e (maybe yum install nano before). Nano is far less powerful than vim, but more user-friendly. Commented Jan 21, 2014 at 18:40
  • 1
    OP, if you have another question, please ask it by clicking the Ask Question button. Commented Jan 30, 2014 at 23:18

2 Answers 2

2

Simply change the working directory:

*/5 * * * * cd /home/myusername/Dev/cron && /usr/bin/python python_sql_image.py
answered Jan 30, 2014 at 23:44
Sign up to request clarification or add additional context in comments.

Comments

0

Each line that contains a job must end in a newline character.

answered Jan 21, 2014 at 18:49

2 Comments

This was plausible as an answer to the original question, however the poster then went and edited the question to be a substantially different one, something they really aren't supposed to do.
@ChrisStratton: roll it back to the previous version, and let the OP know what to do instead?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.