I want to run a python script at boot of Lubuntu 15.04. This python script writes some string into a text file placed in /home/aUser/aFile.txt
My /etc/rc.local file is:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
And the script /home/aUser/theScript.py is:
#!/usr/bin/python
f = open('/home/aUser/aFile.txt','w');
f.write("Some string...");
f.close();
Actually the python script does more, and run an infinite loop, this is why I run the script in background with &. Of course I have python installed:
~$ python --version
Python 2.7.9
I checked if /etc/rc.local is called at boot, and it is, proof of that: I added a test into the /etc/rc.local in this way:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
echo "Test" >> /home/aUser/aTest.txt
and the file /home/aUser/aTest.txt is written/created at boot.
So everything looks correct, proof of that:
if I run manually ~$ /etc/rc.local the file aFile.txt is correctly written.
Instead if I start (or reboot) the OS, the file is not written at boot.
I suspect that could be a problem of permissions/user: I know that /etc/rc.local is run as root, but even if I set root or aUser as owner of the file, the situation is the same. Also run the python script in the /etc/rc.local as user aUser (with su command) does not solve the problem.
Ok I found the problem and fix it, thanks to the #Zac comment.
Actually the python script try to open a network connection before writing the file: at boot time, when the python script is run from /etc/rc.local (so, it is run), the network is still not ready (probably because it is a wireless network) and therefore an exception is raised and the entire script stops. Capturing the exception solves the problem.
So at the end it was my fault, (not) helped by the rc.local that does not provide an easy way to debug.
Related
In crontab I have:
#reboot /usr/bin/python3 /root/mydirectory/mypythonfile.py
Using: which python3 I get: /usr/bin/python3
at the start of the python script I have: #!/usr/bin/python3
The python file is executable for everyone.
Running ./mypythonfile.py in shell works o.k.
I have no idea left what to do, to make this file execute via crontab. I played in crontab and executed some commands (not python script) and it worked fine.
I am logged in as Root, so this should not be a problem as well.
EDIT:
How do I know it's not running?
I created a script, which sends e-mail, inserts dummy data in database and I checked Htop for .py file. Zero on all.
I'm trying to run a python script on my raspberrypi using cron.
I did the following:
crontab -e # To edit a crontab job
After the cron file opened, I added the following line:
#reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!
If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.
What's strange:
I checked the log file and it's empty, so it seems like everything goes fine
If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log
I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!
The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:
#reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log
Some other possible reasons it might not work:
working directory issue (if you're using relative paths)
a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
the script does not output anything (would need some more details about the script)
Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1
How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).
I am working with Ubuntu 16.04 on an NVIDIA TX2 system and have a shell script launcher.sh containing the following commands:
#!/bin/sh
cd /home/nvidia
sudo python test.py >> /home/nvidia/test_output.txt,
basically implying to store the output of test.py in test_output.txt.
The test.pyhas one line: print "Testing startup script!"
I want this script (and more complicated scripts later on) to run automatically when the system boots up. To this end, I added it to the Startup Applications with the command /home/nvidia/launcher.sh and rebooted the system. The file test_output.txt is indeed created but there is no output written in it. I tested the script from the terminal and it works fine.
How do I make this to work from Startup Applications?
Init scripts often don't have the complete environment initialized, e. g. PATH. In order to be executed you better provide the complete paths to commands.
On the other hand sudo is unnecessary here, as you obviously don't do anything that needs root permissions. If you need it, be aware that sudo is asking interactively for a password, which stalls the execution of the script, if the command you try to execute with it isn't explictely permitted in /etc/sudoers.
Provided test.py is located in /home/nvidia, and python in /usr/bin, the script should read
#!/bin/sh
cd /home/nvidia
/usr/bin/python test.py >> test_output.txt
And if you want to add error output to the outfile, which may be useful for debugging, make the last line
/usr/bin/python test.py >> test_output.txt 2&>1
or for a separate error log
/usr/bin/python test.py >> test_output.txt 2> test_error.log
I'm trying to make a Python script run as a service.
It need to work and run automatically after a reboot.
I have tried to copy it inside the init.d folder, But without any luck.
Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)
(Running Centos)
run this command
crontab -e
and then add
#reboot /usr/bin/python /path/to/yourpythonscript
save and quit,then your python script will automatically run after you reboot
There is no intrinsic reason why Python should be different from any other scripting language here.
Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.
For Ubuntu Variant:-
open the /etc/rc.local file with:
nano /etc/rc.local
add the following line just before the exit 0 line:
start-stop-daemon -b -S -x python /root/python/test.py
or
Give absolute path of your command i.e
nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &
The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.
To check and Run
sudo sh /etc/rc.local