Im running python script in background on ubuntu linux with this command:
python script.py &
When Im closing terminal, its run on background
But how I can watch what script.py output, when opening terminal again?
There is many options for monitor the script in background.
First, run Screen command if you would like to disconnect the terminal. If you disconnect your terminal your script process will be an exit.
$ screen
$ python script.py &
Disconnect your terminal
$ screen -ls
Copy the PID and run the below command
$ screen -r PID
You are able to see your script output.
Several Options:
Use a tool like tmux
Use tail -f on the log file
Direct stdout to a file and use tail -f on it
python script.py > /tmp/logfile &
then later:
tail -f /tmp/logfile
Use screen to run your python script
Related
I am using ttyecho (can be installed with yay -S ttyecho-git) to execute a command in a separate terminal like so:
urxvt &
sudo ttyecho -n /proc/<pid-of-new-urxvt>/fd/0 <command>
It does not work because the /proc/pid-of-new-urxvt/fd/0 is a symlink that points to the /dev/pts/x of the parent terminal.
In the spawned urxvt I happen to run zsh. So if I use the pid of that zsh process it works:
sudo ttyecho -n /proc/<pid-of-new-zsh-within-new-urxvt>/fd/0 <command>
How can I get the pid of the new zsh process spawned within the new urxvt process when I run urxvt & ? Or is there a different solution to achieve the same result?
pgrep -P <pid-of-new-urxvt> gives the pid of the child zsh process.
Thx to #user1934428 for the brainstorming
Here is the resulting bash script:
urxvt &
term_pid=$!
# sleep here makes us wait until the child shell in the terminal is started
sleep 0.1
# we retrieve the pid of the shell launched in the new terminal
shell_pid=$(pgrep -P $term_pid)
# ttyecho executes the command in the shell of the new terminal and gives back control of the terminal so you can run further commands manually
sudo ttyecho -n /proc/${shell_pid}/fd/0 "$#"
So when I launch "script ls" it opens a new terminal, runs ls, and gives back the prompt with the terminal still open.
I just had to add ttyecho in the sudoers file.
I have created file one which runs a flask server, starts the Alexa sample app and another is a cmdline command which runs port forwarding server.
which are:
led.py, sudo bash startsampleapp.sh, and the command is: autossh -M 0 -R 80:localhost:8000 serveo.net
So I want to run all these three from one file I tried using subprocess, Multiprocessing and os but didn't seem to work it doesn't go ahead of led.py. And this 3 should run simultaneously because to start Alexa I need to run Alexa sample app by this command: "sudo bash startsampleapp.sh" and when we ask Alexa to do something serveo.net will get the request and it will forward it to led.py(flask server).so Please help me regarding this.
I have tried creating a bash file like this:
#!usr/bin/bash
python led.py &
sudo bash startsampleapp.sh &
autossh -M 0 -R serveo.serveo.net:localhost:8000 serveo.net
It should start them in the parallel process but don't work.
The solution I found was opening 3 terminal and running that 3 things respectively.
I create a bash file main.sh which contained:
#!/bin/bash
lxterminal -e python led.py &
lxterminal -e autossh -M 0 -R serveo.serveo.net:80:localhost:8000 serveo.net &
lxterminal -e sudo bash startsampleapp.sh
Thanks all.
Killing them can be done through getting the process ID and using sudo killall -9 ID command.
If i add a launcher in .config/upstart like:
start_python.conf
start on desktop-start
exec python myscript.py
stop on desktop-end
myscript.py is basically a neverending while-loop that does some voodoo...
If i try the above will it start when the machine boots up.
But it is invisible (can be found with ps aux | grep python after boot).
I want it launched in a visible terminal window so i can monitor it. How do i do that?
Your terminal application should have option accepting command to run within, see man terminal-app-name or terminal-app-name --help. There is Konsole example:
konsole -e python myscript.py
I suppose you have gnome-terminal as a default terminal emulator, so either
gnome-terminal --command="python myscript.py"
or
gnome-terminal --execute python myscript.py
should work.
I'm pretty new to Linux / Raspberry PI.
I want to run a command from a shell script in a new shell window since commands like "cvlc music.mp3" (VLC PLAYER) would block the shell until playback has beenn finished.
Therefore it would be nice to export the playback command to another shell
Is this correct?
gnome-terminal && lxterminal don't seem to be an option for the distribution
for testing purpose I created two dumnmy shell-scripts:
[start.sh]
#!/bin/sh
lxterminal\
--title="MyScriptWindow" \
-e "bash -c ./exe.sh;bash"\
[exe.sh]
#!/bin/sh
echo "Hello World"
[output]
root#raspberrypi:/home/pi# ./start.sh
(lxterminal:1315): Gtk-WARNING **: cannot open display:
If I've understood correctly, you are doing all this only because you want the shell to be released at the execution of your cvlc.
You only need to detach it from shell standard output and run it as a background process
nohup cvlc music.mp3 &
is this enought ?
You could also run the program in background
$> ./test.sh &
Or use screen
Using these command you wont block your shell.
I was using
nohup ./program_name &
to run my program, program_name prints out some values and status of the running process including how much percentage the program has finished, but since I'm running it using nohup so I can't see how close my program to finish is, is there anyway I can still get that information?
We have to Just open nohup.out to see output. Probably you want
tail -f nohup.out
for streaming output
Perhaps adjust your nohup command line to capture all output to a file:
nohup ./program_name > /tmp/programName.log 2>&1 &
Then, you can monitor programName.log using tail:
tail -f /tmp/programName.log
Put the below command in current terminal where the program is running
jobs command used to lists the jobs that you are running in the background and in the foreground
jobs -l
[6]+ 6069 Running nohup perl test1.pl &
[6]+ 6069 Done nohup perl test1.pl