Can't seeing python script output in nohup.out file - python-3.x

I have a bash script that executes in loop a python script:
while true; do python3 script.py && break; done
launched with nohup:
nohup ./run_script.sh &
Why if I run the command:
tail -f nohup.out
I don't see the output of the python script?
I correctly see the script in my running process:
pi 2757 1.5 3.4 37268 33064 ? S 15:06 0:18 python3 script.py
pi 2819 0.0 0.0 1908 388 ? S 15:07 0:00 /bin/sh ./run_script.sh
pi 2820 1.6 3.5 37268 33096 ? S 15:07 0:18 python3 script.py
If I directly launch the python script with nohup, I see the output but I need to relaunch the script everytime it fails, so I need to lauch it with the bash script
Maybe I'm missing some concept concerning nohup usage.

I resolved modifing the script in this way:
while true; do python3 -u script.py && break; done
The output file nohup.out during execution remains empty until the execution is finished. This happens because of output buffering. Adding the -u flag I can avoid output buffering.
For other details see this blog page https://janakiev.com/blog/python-background/.

Related

Getting Process ID Linux using pgrep

I have a bash script called run.sh which launches two python scripts meter_1.py and meter_2.py
#!/bin/sh
./meter_1.py &
./meter_2.py &
When the scripts are running and I search for the PID of the scripts using the command
ps -aux | grep python
The output is
openhab+ 9328 84.0 1.6 25320 16580 pts/0 R 22:23 0:04 python ./meter_1.py
openhab+ 9329 84.6 1.6 25320 16596 pts/0 R 22:23 0:04 python ./meter_2.py
Using the pgrep command I can get the the PID
>pgrep python
9328
9329
However I could have multiple python scripts running and I want to get the process ID name by the script it is running not based on if it is python or not.
For example:
>pgrep python" "./meter_1.py
9328
Is there a functionality for this in pgrep? The following seems to work however it would be nice to get just the process ID back.
>ps -aux | grep python" "./meter_1.py
openhab+ 9328 84.0 1.6 25320 16580 pts/0 R 22:23 0:04 python ./meter_1.py
In ba(sh) you can get the PID of the last started process with $!
so in your run.sh script you can simply use:
#!/bin/sh
./meter_1.py &
echo PID of process1: $!
./meter_2.py &
echo PID of process2: $!
If you have control of the process, like you're the one starting the process in a script, to expand on what have posted already.
#!/bin/sh
./meter_1.py & meter_1_py_pid=$!
./meter_2.py & meter_2_py_pid=$!
The pids are in the variables $meter__1_py_pid and $meter__2_py_pid
You can do whatever you want with the pids, check if it is running, kill it and so on.

How to verify scheduled run in linux

I am following this post. I want to run a python script, in the background, after logging out of ssh, witht the output stored into a specific file. Namely, I wish to use the following bash command:
nohup python3 main.py --dataset CorrSR/testTraining/small --train --input_height=256 --output_height=256 --epoch=2 | at 1:25 PM Mon > logs/background_run_small.txt &
I am not sure regarding the order of the command. is | before >? The command runs with no errors, though a process is immediately opened with
4285 pts/5 Sl 0:02 /usr/bin/python3 -u /usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/start_ipython_kernel.p
and also the output file is immediately created. Is that normal? how do I know the program waits for the designated time to run?
Your command line executes main.py immediately.
What you probably want is:
echo 'nohup python3 main.py --dataset CorrSR/testTraining/small --train --input_height=256 --output_height=256 --epoch=2 > logs/background_run_small.txt' | at "1:25 PM Mon"

package Shell: System commands doesn't work with Centos 7.4

Shell module, Shell.pm, does not seem to run shell commands with Centos 7.4.
For instance following script is OK with Centos 6.4:
#!/usr/bin/perl
use Shell qw(ps);
$cmd=ps;
print $cmd . "\n";
Result is as expected:
PID TTY TIME CMD
29090 pts/1 00:00:00 bash
29325 pts/1 00:00:00 test.pm
29326 pts/1 00:00:00 ps
But with Centos 7.4
#!/usr/bin/perl -I /usr/share/perl5/CPAN
use Shell qw(ps);
$cmd=ps;
print $cmd . "\n";
Result is:
ps
If i add to the previous script:
cat("/etc/passwd");
Following error is raised:
Undefined subroutine &main::cat called at ./test.pm line 10
With a real script none of system commands are well interpreted. Should I rewrite everything with system('command')!?
Finally I succeeded to make it work !
Installation was not quite good.
I had to run :
cpan App::cpanminus
then
cpanm Shell

Cron Job Killing and Restarting Python Script

I set up a cron job on a linux server to kill and restart a python script (run.py) every other day. I set the job to run as root, but I find that sometimes it doesn't kill the process properly (and ends up running two scripts in a row).
Is there a better way to do this?
My cron job parameters:
0 8 * * 1,4,7 cd /home/myUser && ./start.sh
start.sh:
#!/bin/bash
echo "Running..."
sudo pkill -f run.py
sudo python run.py &
I guess run.py runs as python, not run.py. So you won't find anything with kill -f run.py.
You should echo the PID of the process to a file and use that value to kill the previous process if it's still running. Just add echo $! >/path/to/pid.file as the last line in your start.sh script to do so.
Read more:
https://serverfault.com/questions/205498/how-to-get-pid-of-just-started-process
How to read a file into a variable in shell?
http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
Example to get you started:
#!/bin/bash
echo "Running..."
sudo pkill -F /path/to/pid.pid
sudo python /path/to/run.py &
echo $! > /path/to/pid.pid
Another alternative to this is making the python script run on upstart if you are on a system that supports upstart. Then you can just do sudo /sbin/start job_name at the begin and sudo /sbin/stop job_name this makes upstart manage the pids for you.
Python upstart script
Upstart python script

Running a php script in the background via shell - script never executes on mac os x

I have a php script which is responsible for sending emails based on a queue contained in a database.
The script works when it is executed from my shell as such:
/usr/bin/php -f /folder/email.php
However, when I execute it to run in the background:
/usr/bin/php -f /folder/email.php > /dev/null &
It never completes, and the process just sits in the process queue:
clickonce: ps T
PID TT STAT TIME COMMAND
1246 s000 Ss 0:00.03 login -pf
1247 s000 S 0:00.03 -bash
1587 s000 T 0:00.05 /usr/bin/php -f /folder/email.php
1589 s000 R+ 0:00.00 ps T
So my question is how can I run this as a background process and have it actually execute? Do I need to configure my OS? Do I need to change the way I execute the command?
"T" in the "STAT" column indicates a stopped process. I would guess that your script is attempting to read input from stdin and is getting stopped because it is not the foreground process and thus is not allowed to read.
You should check if the script does indeed read something while executing.

Resources