Linux. launch python script from .config/upstart/ in a visible terminal - linux

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.

Related

Shell script: inject command into existing running terminal process

I have a situation where there is a running terminal process. I can find the PID of the terminal. I want to inject a command s.t. it runs and prints output in the target terminal process. I tried the following:
# Assuming terminal with PID is running in a different window
$ echo "ls\n" > /proc/PID/fd/0
This just prints the string in the second terminal window. How can I send a command (like ls) and then an enter key press into the second terminal window ? Thanks
If you have xdotool installed ;
$ xdotool search --pid 2981 # 2981 being the PID of terminal, not shell
60817444
$ xdotool windowfocus --sync 60817444 type $'ls\n'

In a script how to get the pid of spawned terminal's shell to execute commands in it using ttyecho?

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.

How to watch python script which runs in background?

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

Raspbian (jessie) open new terminal window

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.

Open gnome-terminal on an network namespace and then run a command

I'm using coreemu to to create a virtual network. I can open a terminal on a specific node with
gnome-terminal -e "vcmd -c /tmp/pycore.49330/n1 -- bash"
but I would like to open the terminal there, and then have that terminal run a command. I've played around, trying to get the terminal to open and just "echo test" but nothing I've tried works. Any help is appreciated. Thanks
Try
gnome-terminal -e "vcmd -c /tmp/pycore.49330/n1 -- bash -c 'date; read'"
to execute date.

Resources