bash script in rc.local different result than executing it in terminal - python

I'm trying to run a python script from a headless raspberrypi that uses the pynput library.
In order to use pynput on a headleass machine it requires you to do the following steps on every bootup:
export DISPLAY=:0
DISPLAY=:0 python -c 'import pynput'
If I don't do this I get the following error:
ImportError: this platform is not supported: ('failed to acquire X
connection: >
Try one of the following resolutions:
Please make sure that you have an X server running, and that the
DISPLAY env>
If I now try to automaticate this process through a bash script that contains the following lines:
#!/bin/bash
sleep 60
export DISPLAY=:0
sudo chmod 666 /dev/hidg0
DISPLAY=:0 python -c 'import pynput'
python3 /home/pi/Hid.py |& tee -a /home/pi/test.log
exit 0
And execute the file normally like this from terminal:
bash /home/pi/bash.sh
it works perfectly fine.
But when I mention the script in the rc.local file the same way I executed it before, the script is getting executed on boot, but the python script throws the error that was shown at the beginning.
How can there be two different outcomes, when it's getting executed by hand and when it's getting executed by a bash script at boot via. rc.local?
Any help is appreciated!

All commands from rc.local are getting executed by user root.
To execute them from a different user use:
sudo su USERNAME -c 'SCRIPT/PATH'

Related

How to run bash commands from Python preferably with the os library?

I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:
import os
os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')
os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')
os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')
I am running the Python script using the following in the terminal:
python3 test.py
The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:
sh: 1: source: not found
The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .
Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that):
https://stackoverflow.com/a/62355400/11535508
source is a bash built-in command, not an executable.
Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').
The second option is to write your commands into a separate bash script and call it from python:
os.system('bash script.sh')

crontab won't run os.system python command

Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)

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

Using cron to start a python script at every boot

I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron
I made a python script "hello.py":
#!usr/bin/env python
import time
print "Hello World!"
time.sleep(10)
Then chmod +x hello.py.
I checked,if it running or not,it is giving me output.
I used crontab -e command and added the line #reboot python /home/pi/hello.py &.
Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!
Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:
12 0 * * * python /Path/To/File.py > /Path/ToLog/log 2>&1
Then you can check this log timestamp and for its contents

Add CRON via shell to run Python

My cron doesn't seem to execute every 5 mins. Can anyone show me where I have gone wrong?
I made it executable using this command:
chmod +x /etc/utilities/poll.py
I can run it manually with this command:
cd /etc/utilities
python poll.py
When I run it like this I get an error:
root#li453-78:~# /etc/utilities/poll.py
-bash: /etc/utilities/poll.py: Permission denied
This is the command I use to add it to shell (via my automatic deployment script):
crontab -l | { cat; echo "*/5 * * * * /etc/utilities/poll.py"; } | crontab -
The start of my python file is like this:
#!/usr/bin/env python
So, could someone please enlighten me about how I should be adding the cron to my debian server via shell so that it executes?
Using the help from here, for whatever reason, even though I had the code correct to make the script executable, this line didn't seem to fire in my deployment script, meaning that all I had to do was run it afterwards to make it executable and then everything worked.
Lesson learnt: If you need to do this, the code above works

Categories

Resources