Score:1

My cron jobs aren't running when trying to save the script output to a txt file

ng flag

I am trying to have a python script run twice a day- once at 8:00 AM and again at 8:00 PM. My hope was to store the script output to a txt file on my computer named LOG_{date-time}.txt so I could look at "log" files in case something went wrong with the script. I set up the following cron job:

0 8,20 * * * python3 /script/working/directory/Script.py > /script/working/directory/logs/LOG_"$(date +"%d-%m-%Y")".txt

After setting up the cron job I checked the script after 8:00 PM and noticed my script hadn't run yet (I have it set to update a spreadsheet file and leave a time stamp after each update).

I did a bit of troubleshooting on my own and found that if I ran the cron job without the output portion of it, it would work (I also found that the cron job logs are put out in UTC instead of my system time, but that is an issue for another time):

0 8,20 * * * python3 /script/working/directory/Script.py

Is there something I'm missing here? Shouldn't this store the scripts output to a file? I am a bit new to things here and have been trying to learn as I go with this but I'm just not sure where to go with this. It would be nice to be able to store the outpupt of my script for debugging purposes.

Wayne Vosberg avatar
bd flag
try removing the extra quotes. i.e. `/LOG_$(date+'%d-%m-%Y').txt`. Also, do you mean `>>` instead of `>`?
Wayne Vosberg avatar
bd flag
Another thought - make sure the the user running the cron job has access to `/script/working/directory/logs`
hr flag
The `%` characters are special in crontabs, and must be escaped. See for example [Unable to execute command in cron](https://askubuntu.com/questions/829818/unable-to-execute-command-in-cron)
Pocketmouse avatar
ng flag
I had it formated slightly wrong, it's supposed to be `/"LOG_$(date +'%d-%m-%Y').txt"`
Score:1
cn flag

The main reason I can assume is either the log path doesn't exist or you don't have permission to write to the log path.

Try executing the command manually instead of the cron

python3 /script/working/directory/Script.py > /script/working/directory/logs/LOG_"$(date +"%d-%m-%Y")".txt

You will most likely see the error in your terminal.

Pocketmouse avatar
ng flag
This is not the case.
Pocketmouse avatar
ng flag
I went in to edit and just realized I hadn't saved now I can't edit: This is not the case. When I manually run the script in both command line and through a .sh file it works without a problem. I have also set up both the `Script.py` and `Script.sh` to have executable permissions with `chmod +x script.py`. I have been looking into this further and found that if I use things like `get` requests I have to have a virtual environment that needs to be invoked in cron for this script to run. I am not sure what this means or how to implement this currently, but like I said I am reading into it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.