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?
Related
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()
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 am trying to launch a GUI application (rhythmbox) on a Ubuntu. In the following I try to explain the chain of executed files.
# Window manager executes first
~/i3wm_cmd_wrapper.sh Window_Name ~/mount_enc.sh
This wrapper uses gnome-terminal to execute stuff. This enables opening a terminal at startup where users can enter information.
# mount_enc.sh launches the following command in the end
bash ~/launch_in_bg.sh rhythmbox
mount_enc.sh does exactly what it is supposed to do when starting from a normal terminal. But I'd like to start it automatically at startup and rhythmbox should be kept open after the script is done.
# launch_in_bg.sh is just doing what it's supposed to
($PRGRM > /dev/null 2>&1) &
I can not get the gnome-terminal to open rhythmbox for me. Also I think my approach is wrong if I want rhythmbox to keep running after the gnome-terminal finishes executing the mount_enc.sh script. Can anybody think of a better solution?
If you open a program from the console (even in background), the process of the program is a child process of the console process and will terminate when the console process terminates.
To keep the program's process running it has to be detached from the console process. Detaching can be done in multiple ways. Some examples:
nohup rhythmbox &
or
rhythmbox & disown
To suppress output, use redirection as in your script.
I have 2 programs that I want to run, programA.py and programB.py. When I run them manually, I have to open separate terminals and type the following commands:
terminal1
python programA.py
terminal2
python programB.py
Each of these programs then output some data on the command line. Lastly, programA.py has to be fully started and waiting before programB.py can start (takes ~2s for programA.py to start and be ready to accept data).
If I am running these programs in Ubuntu, how can I write a bash script that accomplishes that? Right now, I have the following:
#!/bin/bash
python programA.py
python programB.py
This starts programA.py, but because programA.py then waits for input, programB.py doesn't start until you close out of programA.py. How can I change my script to run the two programs simultaneously?
Edit:
Using the advice given by Andreas Neumann below, changing the script to the following successfully launches the two programs:
#!/bin/bash
python programA.py &
sleep 5
python programB.py &
However, when both programs are launched, the code then doesn't work properly. Basically, programA.py is setting up a listening socket, and then creates an interface that the user works with. programB.py then starts afterwards, and runs a process, talking to programA.py over the sockets. When running the above script, programA starts, waits, programB starts, and then programA and B connect, form the interface, but then programB isn't running its background processes correctly.
Updated Answer
If you find my original answer below doesn't work, yet you still want to solve your question with a single script, you could do something like this:
#!/bin/bash
xterm -e "python ProgramA.py" &
sleep 5
python ProgramB.py
Original Answer
If programA is creating a user interface, you probably need that to be in the foreground, so start programB in the background:
{ sleep 5; python programB.py; } &
python ProgramA.py
#!/bin/bash
python programA.py &
sleep 5 # give enough time to start
python programB.py &
I need to run an external C_program using subprocess from a python script.
Here is the tricky parts:
the external program needs to run with super user privileges, and thus I need to ask the user to input his password to make sure he is allowed to run the program.
The external program might run for extremely long time (sometimes days) and the user might need to terminate it manually using CTRL+C
The output of the C_program should be printed to the screen thus the stdout should be piped to the subprocess.
this is what I've tried doing:
try:
proc = subprocess.Popen(c_program, shell=True, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
#needs sudo password to run this program
proc.communicate(getpass.getpass())
except KeyboardInterrupt:
print "you stopped c_program, script will now go on..."
#do other things
unfortunately, this will cause two problems which I cannot seem to be able to fix:
pressing CTRL+C while c_program is being executed will do absolutly nothing (not only won't it stop the execution of c_program or even the python script itself, it is just ignored...)
In case the script does finish its run, the terminal starts behaving... it will not display what the user is typing in but it does respond to it (for example, typing ls -l will not be displayed on the screen, but, after pressing enter, the output of the command will be printed to the screen)
I'm using Python 2.7 on Ubuntu
Please help :)