Bash script that runs during terminal startup not working - python

This is a snippet of code inside by .profile which is run during startup. I am not too sure why it is not source activate py33dev (which switches from default python 2.7 to 3.3) and cd to desktop does not work, even though the first cd and echo works.
cd /Users/anaconda/envs/py33dev/bin
echo 'source activate'
source activate py33dev
cd ~/Desktop/
.....other bash script commands that work....

Related

Create an automatic routine to run several python script, using a shell script

I am trying to create a shell script that executes some python scripts in order.
I have never done something like this and i don´t know if what i am doing is correct.
Before executing the last file i need to activate an enviroment so it has the necesary imports to so it functions correctly.
This is the script i have created (execute.sh):
#!/bin/sh
python path/file_1.py
python path/file_2.py
python path/file_3.py
# activate a virtual enviroment
source /path/env/bin/activate
python path/file_4.py
After this what sould I do?
Just write this line in the terminal?
./execute.sh
That should work. Usually I always do this when running a bash script with Python scripts inside.
#!/bin/bash
. /home/user/.bashrc # Load basic env as we're coming from cron
# Cd into our working directory in case we're not into it already
cd "$(dirname "$0")";
echo "----------------"
echo "Starting processing `date`"
echo "----------------"
conda activate models # Activate my conda environment
# Execute Python script
python script.py
if [[ $? = 0 ]]; then
echo "Succesfull execution!"
else
echo "Something wrong"
exit 1
fi
This is in execute.run, which you then make executable chmod +x executable.run, and run. This will work wherever you are because it will always try to cd into the directory where the script resides, so you can also call it from crontab.

Why doesn't my cronjob execute as expected?

I edit the crontab in Ubuntu 18.04:
crontab -e
50 21 * * * /data/min/query/sync.sh >> logs/tag.log
Then I saved it and exited. I did this at 21:41pm, and I suppose it will execute it at 21:50pm, but it didn't effect. I checked the logs/tag.log, but nothing occurs.
The content of my sync.sh:
#!/bin/bash
cd /data/min/query
echo 'sync tags now ...'
source venv/bin/activate
python utils/tag_counter.py
echo 'updating resource files are done!!!'
I make the script executable by:
chmod +x sync.sh
So that sync.sh can be executed directly. The script does work when executed at the command line, but not working in the cronjobs. This is the first time I use cronjob. Is there anything I am missing in setting up the cronjob?

How to use a shell script to start python script on boot up within an Python environment?

how would I run python script on startup (on Raspberrypi 4B, Raspi_os 64bit) within an python(3.9) environment?
I tried to just add
source envname/bin/activate to launcher.sh
my launcher file looks like this:
cd /home/pi/thefolder
PYTHONPATH=/home/pi/.local/lib/python3.9/site-package
source envname/bin/activate
pyhton3 /home/pi/thefolder/run.py
Apparently the python3 command is not run in the env as I get an ModuleNotFoundError for Pandas. Which is installed in the env.
EDIT1:
if I just create a new shell script with only
source envname/bin/activate
echo Hi Stackoverflow
running after chmod with sh test.sh I get the return
> test.sh 1: source: not found
> Hi Stackoverflow
while I can activate the env in the terminal
EDIT2:
Apparently Ubuntu bin/dash ist used and not bin/sh. Here is post about it
Now I have to checkout how to change it
Edit3:
using :
. envname/bin/activate
echo Hi Stackoverflow
instead of
source envname/bin/activate
echo Hi Stackoverflow
seems to work. Testing in progress
---Final Edit:---
Yes it works. But important, it apparently has to be in the first line otherwise the file envname cannot be found
Using
. envname/bin/activate
echo Hi Stackoverflow
instead of
source envname/bin/activate
echo Hi Stackoverflow
Works. As Ubuntu uses /bin/bash instead of /bin/sh
Apparently the . envname/bin/activate has to be the first command

Sourcing/Activating Python VirtualEnv in a Bash Script

I am trying to write a simple script that will help me activate (source) the virtualenv and set some environment variables at the same time. Below is the current version that does not yet have any environment variables.
#!/bin/bash
if [ -z "$BASH_VERSION" ]
then
exec bash "$0" "$#"
fi
# Your script here
script_dir=`dirname $0`
cd $script_dir
/bin/bash -c ". ./django-env/bin/activate; exec /bin/bash -i"
The trouble with this script is two-fold.
When I run it - seemingly successfully as it changes the command line prefix to (django-env) - it is missing the My-Computer-Name: in front of it. Obviously, it is an indication of something as I typically have (django-env) My-Computer-Name: as the prefix.
It does not activate the virtualenv properly. Namely, when I check which python, I am notified that the virtualenv Python is used. On the other hand, when I check for which pip or which python3, the global system's Python is used.
What can I do to fix these things and have the environment be activated?
I suspect the problem with exec /bin/bash -i — the executed bash could run .bash_profile and .bashrc that change the current environment.
Instead of a shell script that executes shells upon shells you better create an alias or a shell function:
django_activate() {
cd $1
. ./django-env/bin/activate
}
Put it in .bashrc so it will be available in all shells and run as django_activate $venv_dir; for example django_activate ~/projects/work.
The following code does what I intended it to do. I run it with source script.sh
#!/bin/bash
if [ -z "$BASH_VERSION" ]
then
exec bash "$0" "$#"
fi
# Your script here
script_dir=`dirname $0`
cd $script_dir
/bin/bash -c ". ./django-env/bin/activate"

Using cron to launch python script in terminal window after reboot

I am trying to launch a python script automatically after boot. I want to launch it in a terminal window because the program gives important feedback in the terminal.
I've researched many ways to do this including crontab, init.d, rc.local, and /etc/xdg/autostart/myscript.desktop.
When I've tested my script manually, calling rc.local from the terminal works, however none of these work after booting.
I've tried many variations, the latest in crontab:
#reboot sleep 60 && xterm -hold -e sudo python /home/pi/newcode/newcode/boot-test.py
Other variations I tried include (calling my python script from a shell script):
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
and
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
Update:
foo1.desktop (saved at /usr/local/bin/foo1):
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
foo1 (saved at /etc/xdg/autostart/foo1.desktop):
#!/bin/bash
/usr/bin/x-terminal-emulator --command="/home/pi/newcode/newcode/boot-test.py" --title="My foo1"
python script, very simple for now (saved at /home/pi/newcode/newcode/boot-test.py)
sensortype=raw_input("press enter to continue")
Comment:
1. is named foo1.desktop ?
2. is named foo1.sh ?
3. does foo1.sh need to be made executable?
Yes, must be named *.desktop
If you use *.sh, the entry in *.desktop have to be equal
Exec=gksu /usr/local/bin/foo1.sh
Yes, a Bash script have to made executable.
Question: I've test my script (rc.local) manually
Don't use etc/rc.local, it's executed to early for X11.
My /etc/xdg/autostart example:
Create a foo1.desktop with at least the following content:
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
Copy foo1.desktop or create link to foo1.desktop in /etc/xdg/autostart.
Create /usr/local/bin/foo1 with the following content:
#!/bin/bash
/usr/bin/x-terminal-emulator --command="python path_to_your_*.py" --title="My foo1"
Make /usr/local/bin/foo1, executable.
After trying multiple variation the script that worked contained the following:
#!/bin/bash
/usr/bin/x-terminal-emulator -hold -e sudo python /home/pi/newcode/newcode/hms2-v2.6.py
making the script executable by do the following was also necessary:
sudo chmod 755 /etc/xdg/autostart/foo1.desktop
I never got cron or init.d to work

Categories

Resources