I have a python program and I want to run that program all day. I am running this program from command prompt. After some time command prompt becomes inactive. And when I use enter button it becomes active. Any resolution for this.
I use the following command:
python programName.py
My expectation is - Command prompt should not keep inactive.
In Linux:
Use a 'fire & forget' method.
./my_script &
The & will let the script run in the background.
Or set the task as a cronjob.
Another option is to use screen or tmux, and then run the script.
When your OS is Windows:
Use the Task Scheduler to run the task, or create a Windows Service.
Or use a 3rd party tool, but i'm unaware of those.
add a process like this
def inf_loop(arg1):
while True:
datetime.sleep(1)
p = Process(target=inf_loop, args=('',))
p.start()
p.join()
Related
I have a python program where I made a new process using multiprocessing
but the function doesn't seem to run after FreeConsole() "check" is printed but after that none of the codes work under the sendme() and the main function works after it. actually i want the function sendme to run in the background without console
import multiprocessing as mp
def sendme():
import win32console as con
print("check")
con.FreeConsole()
f=open ("hello2.txt",'w')
f.close()
if name=="__main__":
p=mp.Process(target=sendme)
p.start()
print ("main")
The easiest way to Use the shebang line in your python script. Make it executable using the command,
chmod +x python_script.py
Use no hangup to run a program in background even if you close your terminal.
nohup /path/to/python_script.py &
OR
python /path/to/python_script.py &
Do not forget to use & to put it in background.
To see the process again, use in terminal,
ps ax | grep python_script.py
OR
$ jobs
[1]+ Running nohup python_script.py &
If you are using windows then:
On Windows this can be achieved with the pythonw.exe executable. This will run your program in the background, with no visible process or way to interact with it. You also cannot terminate it without a system monitor.. You can check for the details here pythonw.exe or python.exe?
I am running my code from the shell(SSH) in google cloud as
python3 mycode.py
If I close the shell, the computation stops. How can I start a computation and then close the shell(Computation takes a long Time:)).....come back later and see how it is doing.
My code keeps printing results after a certain number of iteration.
Well, in general what you can do is run the code in a way where you can detach from the interactive environment. Using a tool such as screen or tmux. However, Google Cloud Shell is not made for running background tasks, and if i recall correctly, it will terminate after an hour.
You might need to provision a virtual machine to run it on instead. I can recommend using tmux. With tmux, it will be as simple as running tmux and then in the new shell running your script python3 mycode.py. You can then detach using ctrl+b d or simply disconnect. When you reconnect you run tmux attach -dto get back to your script.
I will provide you as well the following possibility:
A Bash Builtin command that you could also use is disown. First, run your process/script in the background (using &, or stopping it with ^Z and then restarting with bg):
$ long_operation_command &
[1] 1156
Note that at this point the process is still linked to the session and in case it is closed it will be killed.
You can the process attached to the session check running jobs in the background:
$ jobs
[1]+ Running long_operation_command
Therefore you can run disown in order to detach the processes from the session:
$ disown
You can confirm this checking the result of your script or command logging in again or checking with top the process still running.
Check also this because it could be interesting, i.e. the difference between nohup foo, foo & and $ foo & disown
I have some fairly time consuming python scripts to run ~3 hours or so on my machine. I don't want to run them concurrently since it might crash my machine. Alone I have more than enough memory but running 5 or so might cause an issue. I am running them remotely so I ssh into my server and run them like this:
nohup python my_script.py > my_output.txt &
That way if my connection gets interrupted I can re-establish the connection and my result is right there. I want to run the same python script a couple times with different command line arguments sequentially so I can run everything I need without me needing to set up the next one every few hours. I could manually code all of the arguments into a python script and do it that way but it seems inelegant. I don't want to have to fiddle with my python script every time I do this. Is there some sort of listener I could use to trigger the next one when one of them finishes?
I'd suggest writing a bash script that runs the python jobs sequentially:
#!/bin/bash
python3 my_script1.py > my_output1.txt
python3 my_script2.py > my_output2.txt
Then nohup that:
nohup ./driver.sh &
You really want to read up on utilities like tmux or screen and just script the while thing.
I'm not advanced linux user. I use ubuntu.
When I start any process from terminal, for example firefox, I type in:
firefox
The process starts and then I need to write another commands in terminal. For example, I want to change directory, but I can't do it, because firefox is started. And I don't want to close it, but want to enable terminal.
Sorry if my explanation is not clear, I do not know english well.
You can start the process in the backgroun with
firefox &
If you start it with
firefox
it will be in the foreground and you can move it to the background with Ctrl+Z (this will put it in the background but the process will freez until you use bg command) then you must execute
bg 1
where 1 is the job id. You can see the job id with command
jobs
If you need to return the process to the foreground you must use
fg 1
where 1 is the job id.
Simply do this here:
firefox &
drops it to the background.
Also check out the commands disown, nohup and fg.
If your command has already started, you can use Ctrl+Z to send a suspend signal to the running process. Then you can use the bg command (passing in %1 to symbolize the first process on the job list) and that will turn it into a background process, as if you had used the & in the original command.
If you end your command line with &, the program will run in the background and you will be able to use the terminal for other commands. Example: firefox &.
To run any command in background just put & at the end of command
I'm working with a tool right now that requires me to putty to a remote host, login, run a series of commands to start an engine, open a new window (and login again) to start a different engine, then open a third window (and again, login) to actually use the tool (leaving the engines running in those first two windows). I'd like to write a shell script to automate the process so that I could just open one window, type "sh whatever.sh" and be off and running, without physically opening the new windows and logging in again. However, I can't find a command to get me from one window to the next. Any thoughts?
You can just background the first processes by adding an ampersand (&) to the command line or pressing Ctrl+Z when it is running (and then enter bg to let the process continue, more information about that with jobs).
If that's not enough, you can create virtual shells with screen or tmux.
If you've redirected X (i.e. you can access GUIs over ssh), you can also just start a new window by executing your favorite (GUI) console program, like xterm, konsole, gnome-terminal, etc.
Are you familiar with jobs on linux?
nohup whatever_1.sh &
nohup whatever_2.sh &
nohup whatever_3.sh &
Or perhaps screen would be of use here:
https://serverfault.com/questions/25301/job-control-and-ssh
See also, nohup:
http://en.wikipedia.org/wiki/Nohup
The bash command opens a Bourne-again shell (bash) session.
Try typing in "konsole". That should open a new bash window and set the focus to it.
On my Ubuntu 18 I just type the command:
gnome-terminal
and a new shell opens... I don't like the above answers because xterm and konsole most likely not already be installed.
Shell script on target machine cannot be aware of putty windows on client machine.
Consider using Screen : http://www.gnu.org/s/screen/ - it is clean and powerful way.
I think you need command line window then write:
$ xterm
# new window started
If you need python in new window:
$xterm python
#now a window will shown with python shell
Another nice option from Xfce's terminal:
xfce4-terminal