sub process check output fails in cron job

I am running a cronjob which calls a python script which has subprocess checkoutput.

I tried a simple script to debug.

import subprocess
import os


command = "/home/sgadamse/history_checker/code/rg "hello""
try:
    result = subprocess.check_output(command,shell=True)
    print(result)
except subprocess.CalledProcessError as e:
    print(e)

Output when ran in the shell: I get the output i needed

[~/history_checker/code]$ python a.py                                                                                                                                 

a.py:command = "/home/sgadamse/history_checker/code/rg "hello""

I created a cron job to run in crontab -e

* * * * * python /home/sgadamse/history_checker/code/a.py

I get this error when its executed:

Command '/home/sgadamse/history_checker/code/rg "hello"' returned non-zero exit status 1

/home/sgadamse/history_checker/code/rg is the complete rg binary path that i downloaded and using it.

Do we need to do anything differently for the the cron jobs to execute the subprocess checkoutput?

Thanks.

Edit: I tried to debug the rg, Its actually running the command but the files its searching is only “stdin” and nothing else any idea why this would happen?

Answer

Here’s man rg:

ripgrep will automatically detect if stdin exists and search stdin for a regex pattern, e.g. ls | rg foo. In some environments, stdin may exist when it shouldn’t. To turn off stdin detection explicitly specify the directory to search, e.g. rg foo ./.

Vixie Cron is one such environment, because for whichever reason it sets stdin to a closed pipe instead of a more typical redirection from /dev/null:

        /* create some pipes to talk to our future child
         */  
        pipe(stdin_pipe);       /* child's stdin */ 

So you should be able to reproduce it in your shell with true | python .../a.py, and fix it by adding a path as suggested in the manual (rg "hello" ./)