D
D
denism3002022-02-21 13:04:42
Task Schedulers
denism300, 2022-02-21 13:04:42

How to run python script from cron?

There is a parser written in Python, I'm trying to add it to Cron so that it runs on schedule.
Added the following command to the task:

cd /home/user/parser/ && source venv/bin/activate && /home/user/parser/run.py >> /home/user/parser/run.log 2>&1

however, the script is not executed.
The same command via SSH works.

in logs /var/log/cron
Feb 21 12:45:01 mycomp CROND[18202]: (admin) CMD (cd /home/user/parser/ && source venv/bin/activate && /home/user/parser/run.py >> /home/user/parser/run.log 2>&1)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
ave_koty, 2022-02-21
@ave_koty

My parser works every hour according to this scenario:
1 */1 * * * cd /home/ubuntu/vk_bot/ && python3 main.py
Maybe something is wrong with your paths after activating the environment?

V
Vadim, 2022-02-21
@Viji

try to add all this in one script myscript.sh

#! /bin/bash

set -ex
cd /home/user/parser/ 
source venv/bin/activate 
python /home/user/parser/run.py >> /home/user/parser/run.log 2>&1

and already in cron write the launch of bash scripta

A
AVKor, 2022-02-21
@AVKor

Write everything with full paths or write PATH in the task

D
denism300, 2022-02-21
@denism300

The problem was in this piece of command:
>> /home/user/parser/run.log 2>&1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question