os.system('exit') in python - python

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?

read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())

The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.

Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

Related

How can I keep my program running when I use subprocess.call() to switch user?

Whenever I run this program, it completes the first function perfectly and then ends the program before doing anything else. How can I allow the other two functions to run?
import subprocess
subprocess.call(["su", "my_user"]) # runs perfectly
print("user switched to my_user") # does not run
subprocess.call(["cd", "../documents/my_code"]) # does not run
By the way, I am running this from a Linux terminal using ipython.
This isn't doing what you think it is. When you run su, just as the function says, this runs as a subprocess. That subprocess will start as my_user and immediately exit, but your Python process is unaffected. You will still be the original user.
You can feed commands into that subprocess, assuming you need to run things as that other user, but your process isn't going to change.
Followup
subprocess.call waits for the command to finish. The su command is going to create a new shell, logged in as the new user, and that shell will present a new prompt to you. You probably thought your Python script had ended and you were back at the original prompt, but that's not the case. The prompt you're seeing is from su, and you are the new user. If you press Ctrl-D, then the su will exit, your script will continue, and you'll see your script type "user switched to my_user". Thus, you are nested several shells deep and don't realize it. ;)

Python and Win10 - run command and repeat when finished

I am currently using os.system('start cmd.exe /k ' + command)
where “command” is a string that opens an exe file with some arguments.
I know I could avoid calling cmd.exe, but to me it’s easier with that.
When the executable stops, cmd doesn’t close and remains idle waiting for another command; what I want to do is: when the execution finishes, make my python script call the same command again in the same cmd window.
EDIT: I am running multiple instances of cmd.exe which open the same exe with different parameters, my aim is to do the same as above for every instance
returncode = os.system(mycommand)
print(returncode)
prints “0” just after running cmd (without waiting for the execution to end)
I don’t really need the exit code, but parsing the text output from the window might be useful to know whether to stop looping the command or just wait some time before running it again

Create a subprocess in a new shell that can be killed at any point

I want to run a terminal command in a separate shell, but I also want to be able to kill/terminate it at any time. I've read some answers here, and they say to do something like process = subprocess.Popen(args=['gnome-terminal', '--command=%s' % cmd]). The problem with this is, calling process.kill() or process.terminate() after the new shell has been opened does nothing. I want to be able to call a function and kill or terminate the new shell process.
The gnome-terminal command is not actually the terminal program; it's a launcher that talks to a factory and asks it to reuse an existing terminal program to open a new window or tab, or to create a new terminal program if necessary.
So, killing this launcher doesn't make any sense. And, even if it would tell you the PID of the actual terminal process, you wouldn't want to kill that, because it could kill a bunch of other terminal sessions.
You can use the --disable-factory flag to avoid this behavior:
Do not register with the activation name server, do not re-use an active terminal.
For more information on the details of gnome-terminal, you probably want to search or ask somewhere else (probably Super User, Unix & Linux, or Ask Ubuntu), but this should be all you need for your problem.

Running a shell script through Fabric which wants to run a background running command

I am a newbie in Fabric, and want to run one command in a background, it is written in shell script, and I have to run that command via Fabric, so lets assume I have a command in shell script as:
#!/bin/bash/
java &
Consider this is a file named myfile.sh
Now in Fabric I am using this code to run my script as:
put('myfile.sh', '/root/temp/')
sudo('sh /root/temp/myfile.sh')
Now this should start the Java process in background but when I login to the Machine and see the jobs using jobs command, nothing is outputted.
Where is the problem please shed some light.
Use it with
run('nohup PATH_TO_JMETER/Jmetercommand & sleep 5; exit 0)
maybe the process exists before you return. when you type in java, normally it shows up help message and exits. Try a sleep statement or something that lingers. and if you want to run it in the background, you could also append & to the sudo call
I use run("screen -d -m sh /root/temp/myfile.sh",pty=False). This starts a new screen session in detached mode, which will continue running after the connection is lost. I use the pty=False option because I found that when connecting to several hosts, the process would not be started in all of them without this option.

Shell script to run task and shutdown after task is done

I want to run a shell script that runs a python program and shutdowns after the program is done. Here is what I wrote
#!/bin/bash
python program
sudo shutdown -h now
This just shutdowns the system without waiting for the program to complete. Is there a different command to use that waits for the program to complete?
What you have in your example should actually only shutdown once the python command has completed, unless the python program forks or backgrounds early.
Another way to run it would be to make the shutdown conditional upon the success of the first command
python command && sudo shutdown -h now
Of course this still will not help you if the python program does anything like forking or daemonizing. Simply try running the python script alone and take note if control returns immediately to the console or not.
You could run the command halt to stop your system:
#!/bin/sh
python program
sudo halt
The python program is running first, and halt would run after its completion (you might test the exit code of your python program). If it does not behave like expected, try to understand why. You could add a logger command before the halt to write something in the system logs.
Alternatively, you can use command substitution like this:
$(command to run your program)
The script waits until the wrapped command finishes before moving onto the next one!
#!/bin/sh
$(python program.py)
sudo shutdown -P 0

Categories

Resources