pickle
pickle

Reputation: 1045

Run cron from specific directory and log time in log file

I have this script that parses images from an external address. I need to make sure that the script is running in a specific directory. I also want to log the time.

This is what I have done for the cronjob:

*/2 * * * * cd /path/to/script; (time /path/to/script/code.sh arg) &>> /path/to/log/time.log)

And my shell script is pretty simple:

#!/bin/sh
timestamp()
{
    date +"%Y-%m-%d %T"
}
echo "###########################"
echo $(timestamp)
python /path/to/script/parser1.py "-s$1"
python /path/to/script/parser2.py "-s$1"

I get mail to my user, but it is just of the $(timestamp) echo. I should have the timestamp and also have output from both parser1 and parser2 updating in my time.log file. I don't know why this isn't working! I can run the cronjob command from the command line and it works.

Upvotes: 0

Views: 1208

Answers (1)

ice13berg
ice13berg

Reputation: 713

So, I think the problem is that cron is running your script, but not from the directory you want to run it FROM. I am not sure, but I believe cron executes scripts from either the /root or / directories.

Try this - Add this right below your timestamp function - modified with the proper path, of course.

DIRNAME=/path/to/directory
LOGFILE=logfile.log
cd ${DIRNAME}
echo $(timestamp) >> ${LOGFILE}

And then remove the current echo $(timestamp) line.

Also, make sure the log file exists, and you have write access to the file.

Upvotes: 1

Related Questions