___ ___ erkimt .sh Script to run a program in python inside Cron ______ qstntxt ___

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

%pre%

So I did the following program in .sh

%pre%

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

%pre%

My program starts execution, it goes into memory and it works for a few seconds and simply stops without error or without leaving the memory.

Could anyone help me generate a file that controls the execution of my program in python if mine is wrong? or would you have any tips on how to make it work?

    
______ azszpr341473 ___

Since you are apparently in %code% , you could dispense with your %code% , passing the responsibility of checking if another process is already running for your %code% code, see:

%pre%

That in turn would create the lock %code% file to control the single instance of the process.

What would allow such a configuration in its %code% :

%pre%

Reference: link

    
______ azszpr341471 ___

I solved the problem as my final cod was

%pre%     
___

0

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

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

So I did the following program in .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 I run through it the program also works but when I include this command in crontab this way:

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

My program starts execution, it goes into memory and it works for a few seconds and simply stops without error or without leaving the memory.

Could anyone help me generate a file that controls the execution of my program in python if mine is wrong? or would you have any tips on how to make it work?

    
asked by anonymous 06.11.2018 / 12:58

2 answers

0

Since you are apparently in Linux , you could dispense with your .sh , passing the responsibility of checking if another process is already running for your Python code, see:

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)

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

What would allow such a configuration in its crontab :

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

Reference: link

    
06.11.2018 / 17:50
-1

I solved the problem as my final cod was

#!/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
    
06.11.2018 / 17:50