How to Exit Linux terminal using Python script? - python

import sys
def end():
foo=raw_input()
sys.exit()
print 'Press enter to Exit python and Terminal'
end()
When we run the program, we should able to exit the Python Interpreter and Terminal itself.
But it only exits python interpreter, not the terminal.
Thanks in advance.

SIGHUP (hang up) will tell the terminal to exit. The terminal should be your script's parent process, so
import os
import signal
os.kill(os.getppid(), signal.SIGHUP)

Instead of running the command from the shell with just the command name, run it with exec which will cause the shell to replace itself with the program. Then when the program exits the terminal window will close as well.
I.e. instead of
$ python ./my_script.py
run:
$ exec python ./my_script.py

Related

running shell command in python under git-bash not working well

I am using python3 under git-bash environment, and sometimes it does not run shell command well.
#!/usr/bin/env python3
import subprocess as sp
print("hello")
print(sp.getoutput("ls -l")) # This works.
print(sp.getoutput("date")) # This hangs and cannot terminate with ctrl-c.
This does not happen when running under normal linux/bash environment.
Then I come across this one: Python not working in the command line of git bash.
I can run using "winpty python ...", however it still cannot terminate even with ctrl-c.
I take back, getoutput("date") hangs but check_output works.
You are needlessly running those commands with a shell.
Prefer this form:
print(sp.check_output(["ls", " -l"]))
print(sp.check_output(["date"]))

Can’t play sound on vlc when running Python script from bash shell script?

I have a Python script that repeatedly plays a sound using the cvlc command. The Python script works fine when run through the command line or from a bash shell script through the command line but when the bash shell script is run automatically at startup, it runs without any sound. I am using the os.system call to run the cvlc command however, I noticed that it is adding “sh -c” to the beginning of the command. I believe this is causing an issue as vlc as won’t play any sound with this command. Does anyone know how to prevent the “sh -c” from being added to the beginning of the command? Below is my code for the Python script and the bash shell script that runs automatically on startup.
Python script:
import is
import time
import multiprocessing
def make_sound():
os.system(‘cvlc air_horn.wav’)
while True:
process = multiprocessing.Process(target=make_sound, name=“Make Sound”)
process.start()
time.sleep(3)
process.terminate()
process.join()
Bash script:
#!/bin/bash
cd path/to/Python/script
sleep 10
python3 make_sound.py

what shall I do to kill a running script and go back to the prompt in python

I use Ctrl+C each time to kill a running script and go back to the prompt. But then I lose the prompt, only to get "KeyboardInterrupt".
How should I kill a running script and get the python prompt back?
If you're saying that you want to launch the Python interactive terminal after a script finished running (both normally and by keyboard interrupt), then just launch Python with the -i tag. For example: python -i <script-name> <script-args>

How to write a python script (on linux) that executes another script and exits?

I want script a.py to execute script B.y, then exit immediately.
script B.y is then to continue running indefinitely and regularly as if run from the command line.
Target system is Linux Centos if it makes any difference
I guess Popen subprocess is what you are looking for, i.e.:
For windows, something like:
import sys ,subprocess
subprocess.Popen(["C:/Python27/python.exe", "C:/path/to/script.py"])
sys.exit(0)
For linux, just change the path:
import sys ,subprocess
subprocess.Popen(["/usr/local/bin/python", "/path/to/script.py"])
sys.exit(0)
Note:
To find python location on linux, you can use which python

Python: Quit IDLE after Script running

Is it possible to somehow quit Python IDLE under Windows from within my python code/script at the end after running?
I tried something like:
import sys
sys.exit(0) # or exit(1)
didn't work. Probably only quits the current "process" of the running python script.
Thanks a lot
If you can run IDLE from the command line (or edit your shortcut), I found this in the IDLE help.
If IDLE is started with the -n command line switch it will run in a
single process and will not create the subprocess which runs the RPC
Python execution server.
Which seems to imply that the script is running inside IDLE. I did a quick test script, and calling exit() brought up a box asking to kill the program. Clicked yes, and the script and IDLE both quit. Hope that can help.
This will do exactly what you want. No prompt.
import os
os.system("taskkill /f /im pythonw.exe")
To quit Python IDLE under Windows from within a python code/script without a further prompt, try:
parent_pid = os.getppid() # Get parent's process id
parent_process = None
for proc in psutil.process_iter(attrs=['pid']): # Check all processes
if proc.pid == parent_pid: # Parent's Process class
parent_process = proc
break
if parent_process is not None:
parent_process.kill() # Kill the parent's process
This works with IDLE, PyCharm and even the command line in Windows.

Categories

Resources