Script . sh to run a python script from within Cron

Asked

Viewed 1,544 times

2

Good morning. I’m having a problem with a python program I did. What happens is that when I run my program from the command line with this command it works normal :

python3 /home/linaro/programa/main.py > /home/linaro/log.txt &

then I did the following program on . sh

#!/bin/sh
ps -C 'python3 /home/linaro/programa/main.py' > /dev/null
if [ $? = 0 ]
   then
      // programa rodando
   else
      //echo "Iniciando o programa" > /home/linaro/log.txt
      python3 /home/linaro/programa/main.py > /home/linaro/log.txt
fi

when run by it the program also works but when I include this command in crontab this way:

* * * * * root /home/linaro/gerente.sh

my program starts running, enters memory and works a few seconds and simply stops without error or leaving memory.

could someone help me generate a file that controls the execution of my python program if mine is wrong ? or would have some hint on how to make it work ?

  • It is not clear enough the problem, If the program stops, as it remains in memory?

  • the functioning of it , this is the functions that it should perform are not done. but the process is still in memory.

  • If the process is crashing, there must be something wrong with the code that’s stopping it..

2 answers

1

Since apparently Voce is in Linux, you could dispense with your .sh passing the responsibility to check if there is already another process running for your code Python, look at you:

import fcntl, sys

pid_file = '/var/run/script.py.pid'

fp = open( pid_file, 'w' )

try:
    fcntl.lockf( fp, fcntl.LOCK_EX | fcntl.LOCK_NB )
    print('Em execucao!')
except IOError:
    print('Jah estou em execucao!')
    sys.exit(1)

# ....

print('Finalizado!')

sys.exit(0)

Which in turn would create the lock /var/run/script.py.pid to control the single instance of the process.

What would allow a setup like this in your crontab:

* * * * * root /usr/bin/python3 /home/linaro/programa/main.py > /home/linaro/log.txt

Reference: https://en.wikipedia.org/wiki/File_locking#In_Unix-like_systems

  • I’ll try this way thanks tbm

0

I managed to solve the problem as it went my final Cod

#!/bin/sh
ps -aux | grep -P '[p]ython3 /home/linaro/aifaces/main.py(?! -post)' > /dev/null
var1=$?

ps -aux | grep '[p]ython3 /home/linaro/aifaces/main.py -[p]ost' > /dev/null
var2=$?

if [ $var1 = $var2 ]
        then
                if [ "$var1" = 1 ]
                        then
                                # nao esta rodando, executa programa
                                python3 /home/linaro/aifaces/main.py > /home/linaro/log.txt
                        else
                                #esta rodando, envia keepalive
                                uuid=$(cat /etc/machine-id)
                                curl -s -w -X POST --data uuid=$uuid https://apiweb.com.br/keepalive
                fi
        else
                # so um processo ta rodando mata todos e restarta sistema
                kill $(ps aux | grep '[p]ython3 /home/linaro/aifaces/main.py' | awk '{print $2}')
fi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.