0
  • I want to scedual a task with crontab to run a python file in a specific anaconda environment every day at a certain time.
  • I also have a python script to do so.
  • The pythons script runs if I jsut execute it with python h.py in the anaconda evoronment in terminal. h.py is in the home directory
  • I am usaing Ubuntu 20.04, and i havent refreshed on intalled any new cron or crontab
  • I have tried the following commands to get it work but they just do Nothing (the result should be a folder and it is learly not has been created)
crontab -e

Inside the crontab:

#[long descriptional text]
...
53 13 * * * cd /home/ && /home/user/anaconda3/envs/rapids/bin/python h.py    

this alos does nothing no error message

I have also tried the following solutions

  • 32 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py
  • 34 14 * * * cd /home/Documents && /home/anaconda3/envs/rapids/bin/python h.py 2>&1 https://stackoverflow.com/a/64470729/10270590
  • 44 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py >> ~/cron.log 2>&1
  • not worked with normal anaconda - https://unix.stackexchange.com/a/572951/400960
  • 58 14 * * * /home/Documents && /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
  • 59 14 * * * /home/Documents && /home/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
  • 58 14 * * * /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py
  • 10 15 * * * /home/anaconda3/envs/rapids/bin/python home/Documents/h.py
  • Run this for analytics purpose with no results (no file has been created, no printout in terminal) 36 15 * * * /home/anaconda3/envs/rapids/bin/python -c "import sys; sys.stdout.write('out\n'); sys.stderr.write('err\n')" >> /home/so_test.log 2>&1

I have also read teh following solutions but nothing have realy worked

sogu
  • 2,738
  • 5
  • 31
  • 90
  • 2
    You should use the full path of the python script too e.g: `/home/user/anaconda3/envs/rapids/bin/python /home/h.py`. There's no need to `cd`, if you need to be in `/home` you should [set the `cwd` in the script](https://stackoverflow.com/questions/431684/equivalent-of-shell-cd-command-to-change-the-working-directory). – Alex May 29 '21 at 13:51
  • Thank you I have modified it to this ```58 14 * * * /home/Documents && /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1``` I dont undestand teh links you jsut sent can you describe what exactly I should do? – sogu May 29 '21 at 14:00
  • 1
    You do not need to do anything before the command. So the full cron line should be: `58 14 * * * /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py` with whatever output redirects you want. You should probably also expand the `~` for the log. The link I gave is how to change the current working directory of a python script, it is the equivalent of `cd` but within a python script. – Alex May 29 '21 at 14:05
  • Still nothing happens (added it to the tried solutions). Is there a was to gget an error message becasue this also doesent prints out to my terminal anything ```>> ~/cron.log 2>&``` – sogu May 29 '21 at 14:16
  • 1
    `* 15 * * * /home/user/anaconda3/envs/rapids/bin/python -c "import sys; sys.stdout.write('out\n'); sys.stderr.write('err\n')" >> /home/user/so_test.log 2>&1` this works and outputs both `stdout` and `stderr` to the file `/home/user/so_test.log`. Try adding this to your crontab and see if it runs – Alex May 29 '21 at 14:29
  • Do I need the ```/user/``` part becsaue my anaconda folder is in the home folder directly. – sogu May 29 '21 at 14:30
  • 1
    You should know the path to your python executable, if you've activated your environment run `which python` and that will tell you where the executable file is. That should be the first item after the time spec, for me it's `/home/alex/envs/test_env/bin/python`. yours will be different. I have copied the path that _you_ have given in your question! – Alex May 29 '21 at 14:33
  • 1
    Also, in your updated examples you are missing the first `/` on the path to your python script. – Alex May 29 '21 at 14:35
  • 1
    ```10 15 * * * /home/anaconda3/envs/rapids/bin/python home/Documents/h.py``` -> ```10 15 * * * /home/anaconda3/envs/rapids/bin/python /home/Documents/h.py``` – sogu May 29 '21 at 14:36
  • Generated no files no printouts ```* 15 * * * /home/anaconda3/envs/rapids/bin/python -c "import sys; sys.stdout.write('out\n'); sys.stderr.write('err\n')" >> /home/so_test.log 2>&1 ``` – sogu May 29 '21 at 14:40
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233052/discussion-between-alex-and-sogu). – Alex May 29 '21 at 14:43

1 Answers1

2

If the Python file only need python (not other library)

56 16 * * * /home/MY_ACTUAL_USERNAME/anaconda3/envs/rapids/bin/python /home/MY_ACTUAL_USERNAME/Documents/h.py

If Python file requires other python libraries that is in the anaconda environment:

  • create a SHELL script
nano my_sehell_file_name.sh
  • Example what should be inside the file
#!/bin/bash
#conda activate rapids WRONG
source ~/anaconda3/bin/activate MY_ANACONDA_ENVIRONMENT_NAME #correct
#python Documents/my_python_file_name.py WRONG SEPARATLY GO TO FOLER WHTAN EXECUTE EITH python
cd ~/Documents/folder_where_python_file_is/ #correct
python my_python_file_name.py #correct
conda deactivate
  • start up corntab by

corntab -e

  • ex what you can write to the end of this corntab file
43 21 * * * /home/MY_ACTUAL_USERNAME/my_sehell_file_name.sh
sogu
  • 2,738
  • 5
  • 31
  • 90