简体   繁体   中英

Cron Bash: can't run bash file with cron

I have a Python script which I run from a pipenv environment.

I want to run that every minute using cron, but I have not been successful and the solutions I could find have not worked for me.

First I tried:

* * * * * cd /home/santiago/flaskpostgres/writeText && pipenv run python3 main.py

But I got nothing.

I tried searching for a solution and a bash file was recommended on several places, so I changed the line to be

* * * * * bash /home/santiago/hello.sh

and wrote a bash file named hello.sh that contains

#! usr/bin/bash
cd /home/santiago/flaskpostgres/writeText
pipenv run python3 main.py

I also changed permissions on hello.sh with

chmod +x hello.sh

but also nothing.

The bash file by itself works, I have tested that in isolation, but cron does not seem to run it.

cron does work, when I tested with this line

* * * * * echo "hello" >> /tmp/test.txt

That worked without a problem, its just this python file i cant get to work

In case its helpful the contents of the python file located at flaskpostgres/writeText/main.py are:

from datetime import datetime

with open('sample.txt', 'a') as file_object:
    # Append 'hello' at the end of file
    file_object.write(f"{datetime.now()} \n")
    # Close the file
    file_object.close()

this is sort of a Dummy python file, the real one I want to schedule its a bit more complex and has several side effects, so I can't run it every time I want to test cron.

The server is running Ubuntu 20.04.3

As the comments mentioned it was indeed a PATH thing.

I updated the path of my bash file to be the same as the one its used when interacting with the shell and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM