I have a short Python script that needs to run at startup - Ubuntu 13.10. I have tried everything I can think of but can't get it to run. The script:
#!/usr/bin/python
import time
with open("/home/username/Desktop/startup.txt", 'a') as f:
f.write(str(time.time()) + " It worked!")
(The actual script is a bit different, as I'm just using this for testing purposes, but you get the idea.)
I've tried all of the following, with no luck:
Put the command
python startuptest.py
incrontab
, as@reboot python /home/username/Documents/startuptest.py
, both as the regular user and assudo
Put the command
python /home/username/Documents/startuptest.py
in/etc/rc.local
Opened Ubuntu's Startup Applications and put the command there
Done all of the preceding, putting the command into a shell script and calling that shell script instead
Nothing works. I get the feeling I'm missing something simple. Any ideas? (The script runs fine if I just run the command from a terminal.)
Create file ~/.config/autostart/MyScript.desktop with
It helps me!
In similar situations, I've done well by putting something like the following into /etc/rc.local:
This has worked on multiple versions of Fedora and on Ubuntu 14.04 LTS, for both python and perl scripts.
Put this in
/etc/init
(Use/etc/systemd
in Ubuntu 15.x)mystartupscript.conf
By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.
manual starting/stopping is done with
sudo service mystartupscript start
andsudo service mystartupscript stop
Instructions
Copy the python file to /bin:
sudo cp -i /path/to/your_script.py /bin
Add A New Cron Job:
sudo crontab -e
Scroll to the bottom and add the following line (after all the
#'s
):@reboot python /bin/your_script.py &
The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up.
Test it:
sudo reboot
Practical example:
Add this file to your Desktop: test_code.py (run it to check that it works for you)
Run the following commands:
sudo cp -i ~/Desktop/test_code.py /bin
sudo crontab -e
Add the following line and save it:
@reboot python /bin/test_code.py &
Now reboot your computer and you should find a new file on your Desktop:
HERE.txt
If you are on Ubuntu you don't need to write any other code except your Python file's code , Here are the Steps :-