shell start / stop for python script
It is because ps aux |grep SOMETHING
also finds the grep SOMETHING
process, because SOMETHING matches. After the execution the grep is finished, so it cannot find it.
Add a line: ps aux | grep -v grep | grep YOURSCRIPT
Where -v means exclude. More in man grep
.
How to kill python script with bash script
Use pkill
command as
pkill -f test.py
(or) a more fool-proof way using pgrep
to search for the actual process-id
kill $(pgrep -f 'python test.py')
Or if more than one instance of the running program is identified and all of them needs to be killed, use killall(1) on Linux and BSD
killall test.py
How do I kill and restart a python script at regular intervals? Example python script included in description
You could do this through crontab
(see also man crontab
). The script will be a simple bash script like e.g. the following:
#!/bin/bash
# kill running script
ps ax | grep bot.py | grep -v grep | awk '{print $1}' | xargs kill -9
# restart
/path/to/your_script.py & disown
The crontab entry (edit with crontab -e
) should look like this:
*/5 * * * * /path/to/bash_script.sh
A much easier solution however IMHO would be to just set a hard resource limit with ulimit
(see help ulimit
in a bash shell) on the memory that can be used by the process, so that it will be killed whenever it exceeds the limit. Then, simply call the script in a loop with a bash script:
#!/bin/bash
# limit virtual memory to 500MB
ulimit -Sv 500000 -Hv 500000
while true; do
/path/to/your_script.py
sleep 1
done
Linux start-stop-daemon directory error calling shell/python script
Try calling cd
using an absolute path, for example /home/alexjg/
instead of ~/
; the reason it was broken before is that in your example you're using sudo
which keeps the home directory of the user running it. However when you're calling the bash script from init it will use root's home directory instead which doesn't contain test.py
.
The file is created because the redirection is still succeeding; however because starting Python failed there was no output.
How to run & stop python script from another python script?
You can use python Popen objects for running processes in a child process
So run('ABC.PY')
would be p = Popen("python 'ABC.PY'")
if ScriptRunning('ABC.PY)
would be if p.poll() == None
stop('ABC.PY')
would be p.kill()
Bash script that starts python script doesn't stop itself
The parent bash script will remain as long as the child (python script) is running.
If you start the python script running in background (add & at end of python line) then the parent will exit.
#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &
If you examine the process list (e.g. 'ps -elf'). It will show the child (if still running). The child PPID (parent PID) will be 1(root PID) instead of the parent PID because the parent doesn't exist any more.
It could eventually be a problem if your python script never exits.
You could make the parent script wait and kill the child, e.g. wait 30 secs and kill child if it is still present:
#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &
sleep 30
jobs
kill %1
kill -9 %1
jobs
Programmatically stop execution of python script?
sys.exit() will do exactly what you want.
import sys
sys.exit("Error message")
How to stop/terminate a python script from running?
To stop your program, just press Control + C.
Start and stop a Python script at a specific time
If I've understood what you're asking, you could use while loops. Have it periodically check the current time with the times for the shift beginning and end. Below is a guide of what I mean, but I'm not sure the comparisons will work.
from time import sleep
def start():
time_periods = working_periods_table.find_all()
today = datetime.datetime.now().isoweekday()
for day in time_periods:
if day["day"] == today:
start_time = day["work_start_at"]
end_time = day["work_end_at"]
while True:
if datetime.datetime.now() >= start_time:
while True:
schedule.every().week.at(start_time).do(record_activity(idle_time=300))
if datetime.datetime.now() >= end_time:
break
break
else:
sleep(100)
Related Topics
How to Declare Custom Exceptions in Modern Python
How to Convert Index of a Pandas Dataframe into a Column
How to Check File Size in Python
Background Thread With Qthread in Pyqt
Total Memory Used by Python Process
How to Sort Pandas Dataframe from One Column
How to Extract a Floating Number from a String
How to Join Two Dataframes For Which Column Values Are Within a Certain Range
How to Import the Class Within the Same Directory or Sub Directory
Check If All Elements in a List Are Identical
Accessing Dict Keys Like an Attribute
Find Nearest Value in Numpy Array
How to Read a File in Reverse Order
Convert Timestamps With Offset to Datetime Obj Using Strptime
(Still) Cannot Properly Install Lxml 2.3 for Python, But at Least 2.2.8 Works