My goal is to launch a script when an Ubuntu machine starts up. The script kicks off some scrapers, and none of it appears to run on startup. Looking through the forums, I've tried the applicable the recommended approaches, but perhaps I'm missing a step.
I want this script to run on startup:
#!/bin/sh
conda activate scraper
cd /home/me_user/Documents/scrape
python /home/me_user/Documents/scrape/prod_images.py 2010 > /tmp/images2010.log &
python /home/me_user/Documents/scrape/prod_images.py 2011 > /tmp/images2011.log &
python /home/me_user/Documents/scrape/prod_images.py 2012 > /tmp/images2012.log &
python /home/me_user/Documents/scrape/prod_images.py 2013 > /tmp/images2013.log &
python /home/me_user/Documents/scrape/prod_images.py 2014 > /tmp/images2014.log &
python /home/me_user/Documents/scrape/prod_images.py 2015 > /tmp/images2015.log &
When I run it myself from the command line from a local directory, it starts without issue and all of the python scripts kick off.
That looks like this when I run a copy of it:
. /home/me_user/Documents/scrape/init/launch_prod_images.sh
I wanted it to execute on startup, so I logged in as sudo, copied it to /etc/init.d/ and did the following
chmod 777 /etc/init.d/launch_prod_images.sh
(initially, I did chmod +x on the file, but resorted to opening it all the way up with chmod 777.)
Then, created a symbolic link to
ln -s /etc/init.d/launch_prod_images.sh /etc/rc2.d/
When, I log into the machine, I'd expect to see those scripts running in the Task Manager window, but they aren't. What am I missing?
Thanks in advance!
As #furas suggested, cron is the way to go for now and one cron job for each.
Some important changes which helped:
The correct permissions are set for all of the files being called through execution.
Just do the cd within crontab
Explicit path to python
#reboot cd /home/me_user/Documents/scrape/ && /opt/anaconda3/envs/scraper/bin/python /home/me_user/Documents/scrape/prod_images.py >> /tmp/images2010.log
Related
I recently upgraded to Mac Big Sur and have noticed my Python 3.8 cron jobs have stopped working. Under my own account in a bash shell, I can run this without issues ...
davea$ cd /Users/davea/Documents/workspace/article_project; source ./venv/bin/activate; python3 manage.py check_duplicates
In my crontab, I had this set up, which used to work before the upgrade ...
*/5 * * * * /bin/bash -l -c 'cd /Users/davea/Documents/workspace/article_project; source ./venv/bin/activate; python manage.py check_duplicates >> /Users/davea/logs/record2.txt 2>&1'
However, after the upgrade, I'm noticing my command is never run and I see this issue in my log file
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3: can't open file 'manage.py': [Errno 1] Operation not permitted
These are the permissions/groups on my "manage.py" file ...
davea$ ls -al manage.py
-rwxrwxr-x 1 davea staff 866 Apr 15 2019 manage.py
What else do I need to do to get my cron job to run again?
Turns out with the new Mac OS there is an extra level of permissions that need to be enabled. In System Preferences, under Security and Privacy, I clicked the Privacy tab, and then added "cron" to the "Full disk Access" list
then the cron jobs ran without the permissions error.
I think in this case, python3 is considered as "any user" and with the -rwxrwxr permission it only have right to read the file, try to run chmod 775 manage.py in your folder to add permission to manage.py to be executed by "any user" (rigths should be set to -rwxrwxr), hope it can help.
EDIT: technically, read permission should be enough for python to run a file, but I can't see another reason why this error would appear, and I would be interested if you find one
This looks like a SIP or other mac-specific access permissions error, especially since it was right after an upgrade. Could be: https://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/
I've also had lots of problems working with venvs with cron, could be related to this: https://stackoverflow.com/a/7031758/13113166
It's also weird that the error comes from /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 when i think it should come from your venv if it's activated correctly.
Trying to send some output to Slack using cron on an instance of GCP Compute Engine running Linux Ubuntu 18.04.2 LTS.
Output is generated by python script.
Python script is usually run using conda activate my_env and python my_script.py
I have made the bash file executable by doing chmod +x my_script.bash
Here is content of bash file:
#!/bin/bash
source /home/user/miniconda3/bin/activate
conda activate my_env
python /home/user/folder/cron/reports.py -r check_stocks
I would expect adding the following line to crontab -e:
00 21 * * * cd /home/user/folder/cron/ && /bin/bash my_script.bash would give me the same results.
I run cd /home/user/folder/cron/ && /bin/bash my_script.bash in my shell and the script runs fine.
Make your .py file exacutable too (chmod +x file.py) - otherwise it won't work.
You can find similar issue resolved here.
I had a similar issue and I gave up activating the conda environment and instead called directly the python bin in the miniconda environment folder, like this:
00 21 * * * /home/myuser/miniconda3/envs/my_env/bin/python /home/user/folder/cron/reports.py
I don't think this is the recommended solution, especially if you have a complex project with dependencies and you are importing a lot of modules, but for my simple script it solved the problem.
I succeed in setting up Cygwin Crontab on Windows. I need to run Python script every 5 minutes.
*/5 * * * * run-one C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
Above didn't work in crotab. Also the log file cygstart cron.log is empty. How I'm suppose to fix and run the script?
P.S. Script is running fine using idle editor.
Run your script directly in the Cygwin command line:
C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
If it does not work, you may need to specify python.exe directly and use forward slash ("/") rather than backslash ("\"):
C:/Python27/python.exe C:/Python27/Scripts/myScript.py > C:/Python27/Scripts/myScript.log 2>&1
If it works, then please make sure cron is running. If it is running, it should give similar output as below:
$ cygcheck.exe -c | grep cron
cron 4.1-61 OK
cron-debuginfo 4.1-61 OK
$ cygrunsrv -Q cron
Service : cron
Display name : Cron daemon
Current State : Running
Controls Accepted : Stop
Command : /usr/sbin/cron -n
$ ps -lef | grep cron
SYSTEM 4852 4680 ? 15:16:50 /usr/sbin/cron
If it is not running, run
$ cygrunsrv --start cron
If there is an error, you may need to reinstall cron.
$ cyglsa-config
Then
$ cron-config
And follow the instructions.
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 in crontab, as #reboot
python /home/username/Documents/startuptest.py, both as the regular user and as sudo
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.)
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)
from os.path import expanduser
import datetime
file = open(expanduser("~") + '/Desktop/HERE.txt', 'w')
file.write("It worked!\n" + str(datetime.datetime.now()))
file.close()
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
Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)
mystartupscript.conf
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py
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
and
sudo service mystartupscript stop
If you are on Ubuntu you don't need to write any other code except your Python file's code , Here are the Steps :-
Open Dash (The First Icon In Sidebar).
Then type Startup Applications and open that app.
Here Click the Add Button on the right.
There fill in the details and in the command area browse for your Python File and click Ok.
Test it by Restarting System . Done . Enjoy !!
Create file ~/.config/autostart/MyScript.desktop
with
[Desktop Entry]
Encoding=UTF-8
Name=MyScript
Comment=MyScript
Icon=gnome-info
Exec=python /home/your_path/script.py
Terminal=false
Type=Application
Categories=
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=0
It helps me!
In similar situations, I've done well by putting something like the following into /etc/rc.local:
cd /path/to/my/script
./my_script.py &
cd -
echo `date +%Y-%b-%d_%H:%M:%S` > /tmp/ran_rc_local # check that rc.local ran
This has worked on multiple versions of Fedora and on Ubuntu 14.04 LTS, for both python and perl scripts.
nano /etc/rc.local
and edit in
python ~/path-to-script.py
worked for me
I have written a small python script in Ubuntu 13.10, that when run opens 4 terminals and displays some useful information, is it possible for me to get this to run automatically after I login, I have tried a couple of methods but they all involve it running at start up and this wont leave the terminals displayed.
Bootup:
$ crontab -e
Add:
#boot cd /home/user/; python script.py
Login:
Edit one of these files:
nano /etc/profile.d/myscript.sh
~/.bash_login
And add:
cd /home/user; python script.py
Graphical login:
nano ~/.xinitrc
Add:
eval(cd /home/user; python script.py) &
Ubuntu-Gnome specific login:
nano ~/.gnomerc