Run SH script in a new terminal window - python-3.x

I am currently using crontab to run a SH script at boot which navigates to the path of my python script, switches to a different python environment and runs my python script, although it works perfectly fine it runs hidden without a terminal for me to monitor whatever the python interpreter prints like errors, how could I make it so the python interpreter points at a newly opened terminal window?
Here is my SH script (runs with the bash interpreter, not sh):
#!/bin/sh
cd /
cd /home/pi/Desktop/Juvia-py
source defenv/bin/activate
python3 juvia.py &
and my crontab entry:
#reboot bash /home/pi/launcher.sh
Thank you

If you just want to record errors, you could pipe STDOUT and STDERR to files, something like
python3 juvia.py >stdout.log 2>stderr.log &
But if you wanted to open it in a separate window so you could interact you would need to manage STDIN more creatively.

Related

linux bashrc alias to open terminals and run individual scripts

I am trying to make a shortcut alias that does the following for me:
opens a new terminal and inside that new terminal it cd's to a directory and runs a script.
At the same time, opens another new terminal and inside that new terminal it cd's to a directory and runs a script.
I have tried the following:
alias launchmystuff='cd /path/to/directory1/ && gnome-terminal && ./myscript1.sh; cd /path/to/directory2/ && gnome-terminal && ./myscript2.sh'
There are two problems with this:
It opens 1 terminal and is waiting for myscript1.sh to finish BEFORE launching the second terminal.
The terminal that opens and runs the script does not have "possession" of the script that is running. The original terminal that the alias is typed into has "possession" of the scripts that are running. So if I close any of the newly created terminals, the script will not die.
Ideally I would like two terminals to open at once, each running their unique scripts, and for each of them to kill the script when I close their respective terminals.
Does anyone know how to accomplish this?
Thank you
You need to pass the script as an argument to gnome-terminal, rather than running it in the current shell after starting gnome-terminal. As usual, you should use a shell function instead of an alias here.
launchmystuff () {
cd /path/to/directory1/ && gnome-terminal -e ./myscript1.sh
cd /path/to/directory2/ && gnome-terminal -e ./myscript2.sh
}

Linux (Raspbian) - Running script at login(auto) and keeping window in foreground for interaction

I have tried everything to make this work. login scripts, LXDE-pi autostart, cron task #reboot, init.d, and I cannot get my script running with a terminal window running in the foreground so that I can interact with it. I can get it to run but only in background. Is there any way I can get a script that simply runs: "python /home/pi/myscript.py" at startup and leaves the terminal window open with the script running for my keyboard inputs? I would rather not use the /dev/input/event if at all possible. Thanks
Simply running python /home/pi/myscript.py at startup will run your script without any terminal. So there is no window that can be kept open. The behavior you want can be achieved by starting an terminal application and let it execute your script.
e. g. using xterm:
xterm -e "python /home/pi/myscript.py"
or lxterminal:
lxterminal --command "python /home/pi/myscript.py"
I was missing a simple flag.. what I did was edit ~/.config/lxsession/LXDE-pi/autostart with
#lxterminal -e /home/pi/autoscript.sh
and in that file, I added
cd /home/pi/
python -i 2Trackmain.py
I wasn't using the -i flag, so every time I pressed Enter to move through the interactive py script, it exited the terminal, using -i will keep the window open for your interaction. And I only had to add the change directory part b/c the script called other scripts in the same directory.

Running a bash job within a python script

I was hoping for some advice on using the subprocess module.
I'm trying to run a bash job within a python script so my bash command (in the right directory) is: ./program myjob.inp
This is just running the executable "program" with myjob.inp being the input file (and my python script constantly updates myjob.inp).
I know that if I just wanted to run "program", I could do something like:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program"], stdout = fstore_tmp)
However, I can't figure out how to run the job taking in the input file myjob.inp such that it's doing the equivalent of ./program myjob.inp. I tried:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program", "myjob.inp"], stdout = fstore_tmp)
However, that doesn't seem to be working. Does anyone have any suggestions? Thanks!

Opening new terminal in shell script

I am writing a shell script in Linux. I want to run some sh files in terminal and they all needs terminal themselves.
How can I open a new terminal to run another scrips?
xterm -e script arguments... &

Cron & Virtualenv : Cron does not run a python script that requires a virtualenv

EDIT 2 27/06/2013: The problem was a silly mistake unrelated to venv and cron.
Running venv from cron with the same user that created the venv works great using the activate desscribed below.
EDIT 25/06/2013: Since nohup.out is unchanged after the cron runs, I suspect the problem is in the use of virtualenv.
The cron is set with the same user than the one running the script from the command line.
I have written a script to activate venv then run a python script. It runs perfectly from the command line, when I do
nohup /home/heyheyhey/run.sh &
However, for some black magic reasons, the python part does not run from cron:
0 4 * * * /home/heyheyhey/run.sh &
Content of run.sh:
#! /bin/bash
cd /home/heyheyhey
. /home/heyheyhey/.pythonbrew/venvs/Python-2.7.2/venvname/bin/activate 2> error.log
python /home/heyheyhey/top.py 2> error.log
bzip2 -c "Exporter.csv" > "extraction.csv.bz2"
The run.sh executes since the output compressed file is created.
However the python script does not work since the Exporter.csv is not updated and I do not see activity into the log file.
Thanks for your help!
The environment of a cron job is often different from the environment you see
when you're logged into an interactive shell. In particular, you might want to
check whether the python interpreter is on the $PATH for the cron job. If your
python program reads any environment variables, you should check those too, to
ensure they're set as expected under cron.

Resources