0

I'm trying to run some crawler with Linux crontab.

This should go to the Python environment with

pyenv shell jake-crawler

Here is my crontab -e

*/10 * * * * /home/ammt/apps/crawler/scripts/bat_start.sh

This will run every 10 minutes. This command line works fine when I type

(jake-crawler) [jake@KIBA_OM crawler]$  /home/jake/apps/crawler/scripts/bat_start.sh
[DEBUG|run.py:30] 2017-09-24 19:55:49,980 > BATCH_SN:1, COLL_SN:1, 1955 equal 0908 = False

Inside of bat_start.sh I have init.sh which changes the environment to Python.

Here is my init.sh

#!/usr/bin/env bash

export PATH="${HOME}/.pyenv/scripts:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

pyenv shell jake-crawler

This has no problem when I personally run it from command line. But when cron run it by itself, it cannot find the pyenv command.

Shi
  • 4,178
  • 1
  • 26
  • 31
min moica
  • 139
  • 2
  • 12
  • 1
    When you run it *personally*, do you run it as `ammt` user? What user is used to run cron? Is it `root`? If so, then why do you use `${HOME}` in `init.sh` rather than `/home/ammt`? –  Sep 24 '17 at 11:06
  • I used ammt. how to check ${Home} path in command line? ${Home} is defined as /home/ammt – min moica Sep 24 '17 at 11:15
  • To check it you may say `echo $HOME`. However, from your comment it's unclear whether you only used `ammt` to run the command *manually* or that you also used it to run `cron` as well. Could you please expand on this? –  Sep 24 '17 at 11:18
  • echo $HOME saying /home/ammt. I manually command /home/ammt/apps crawler/script/bat_start.sh after I logon with ammt. and inside of bat_start.sh. I have init.sh with pyenv shell ammt-crawler command line. – min moica Sep 24 '17 at 11:22

2 Answers2

0

I think that you can specify which user should run that script in cron configuration file. So, if that script is working with your user, then define it in your cron configuration filr.

See this answer for example... https://stackoverflow.com/a/8475757/3827004.

Branimir Đurek
  • 632
  • 5
  • 13
  • 1
    A system crontab requires an account name to be specified as the 6th field. An ordinary crontab, one processed by the `crontab` command, does not; the command always runs under the account of the user who created it using the `crontab` command. – Keith Thompson Sep 24 '17 at 20:28
0

There are two things that diferentiate when you launch an application from the terminal, and when you do from a crontab file:

  • the environment is not the same, at least if you don't execute your .profile script from your cron job.
  • You don't have access to a terminal. Cron jobs don't use a terminal, so you will not be able, for example to open /dev/tty. You will have to be very careful on how redirections are handled, as you have them all directed to your tty when running on an interactive session, but all of them will be redirected possibly to a pipe, when run from cron(8).

This makes your environment quite different and is normally a source of errors. Read crontab(1) man page for details.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31