I have a python script /var/www/html/dummy/a.py under Ubuntu 14.04 server 32bit
My script updates my status on twitter every time the server is rebooted
The script in question has the right permissions for execution.
My crontab is as follows:
reboot / usr / bin / python /var/www/html/dummy/a.py > / dev / null
but ... but ... my script does not run
I see the following logs:
Mar 12 09:05:53 dumm cron[890]: (CRON) INFO (Running #reboot jobs)
Mar 12 09:05:53 dumm CRON[904]: (gri) CMD (python /var/www/html/dummy/bb.py >/dev/null)
Can someone help me to know because it does not run?
Let me know if I need to provide more information?
EDIT:
I managed to solve this myself - it is necessary in the script to insert the full path !
SOLVED I managed to solve this myself - it is necessary in the script to insert the full path !
Related
I have installed python3 following the Digital Ocean guide and to run my python scripts just by command line, this is the bash I use and it works:
~$ source /home/username/python_projects/project_name/bin/activate
~$ python3 /home/username/python_projects/project_name.py
~$ deactivate
if I put that commands in crontab in the same order, nothing happens.
0 7 * * * source /home/username/python_projects/project_name/bin/activate
0 7 * * * python3 /home/username/python_projects/project_name.py
0 7 * * * deactivate
What am I doing wrong?
Cronjob is active and running.
~$ systemctl status cron
The python file has this permissions:
-rw-rw-r-- 1 user_name user_name 17075 Feb 7 02:30 python_projects/project_name.py
The activate job runs, then exits, and then the next cron job knows nothing about what it did. You want to run them all in the same process (though running deactivate at the end is pointless, as everything that activate did will be wiped when the job ends anyway).
In practice, you can run the Python interpreter from within the virtual environment directly.
For what it's worth, the error message about "no MTA installed" means that the output from cron was discarded because the system could not figure out how to send it by email. You'll probably want to change the rule to write the output to a file.
0 7 * * * cd python_projects && ./project_name/bin/python3 project_name.py >>project_name.log 2>&1
Perhaps notice that cron always starts in your home directory, so you don't have to spell out /home/username (provided of course that username is the user whose crontab this runs from).
There are a few things that could be causing your cronjob not to run:
Environmental variables: The source command in your cronjob sets up the virtual environment, but this environment is not passed on to the cron process. You should specify the full path to the Python executable in your virtual environment, e.g. /home/username/python_projects/project_name/bin/python3.
Permission issues: Ensure that the cron user has permission to execute the script and access the virtual environment.
Output: By default, cron does not send any output to the terminal. If your script produces output, you may want to redirect it to a file for debugging purposes. You can do this by adding > /tmp/output.log 2>&1 to the end of your crontab entry.
You can check the system logs for any error messages related to your cron job. The logs are usually located in /var/log/syslog or /var/log/messages.
I hope this helps!
I have an issue with cron job on a Raspberry Pi.
I've search the internet and StackOverflow but nothing seems to fix my problem.
I'm new to Linux and think I might have broken something that I don't understant yet.
So : Cron tasks work but not fully.
I have a test script called test2.py writing in a file and printing File created in the console.
When I run this script myself, it works.
When I run this script with cron, it prints the "File created" but won't create the file or write in it.
I watched syslog, cron is running the task and logging in a file. This log file only give me the print output and no other error. File was not created.
Where did I mess this up ?
TLDR : Cron run the task, I can see the script print but not writing in a file.
test2.py :
file = open('testfile.txt','w')
file.write('Hello World')
file.close()
print('File created')
This is the Crontab entry : */1 * * * * /usr/bin/python3 /var/www/test2.py >> /tmp/cron.log 2>&1
Here are the perm on the files :
273281 4.0K -rw-r--r-- 1 pi pi 211 Dec 17 11:32 test2.py
Or, not working either :
273281 4.0K -rwxrwxrwx 1 pi pi 211 Dec 17 11:32 test2.py
Thanks for your help and suggestions.
Tin Nguyen solved this for me.
It was simply a path issue. File is created somewhere. My path was relative and cron directory is not the same as the one I ran the script from.
Thanks a lot.
I am using a virtualbox with ubuntu 20.04. Within the VM I have a condo environment and installed python-crontab using:
conda install -c conda-forge python-crontab
I want to continuously run a certain python file which will write to a database. I can make this happen when using job.run() but not when the job is actually on a time schedule.
I have tried the following things:
crontab -e
then adding in
* * * * * python /home/cron/... to my python file
I have also tried:
sudo crontab -e
then adding in
* * * * * python /home/cron/... to my python file
as well as just making a call to python and running the following:
from crontab import CronTab
cron = CronTab(user=True)
job = cron.new(command='python /home/cron/... to my python file')
job.minute.every(1)
cron.write()
user=True I have also tried replacing by user=cron
Now as mentioned I can run job.run() and have new entries in my database. But when it is on a schedule nothing gets written to my database anymore.
My syslog file says the following:
(cron) CMD (python /home/cron/... to my python file)
(CRON) info (No MTA installed, discarding output)
(root) CDM (python /home/cron/... to my python file)
These messages do get written every minute, so cron does seem to run, why do I not see any output in my database then?
ps with some digging on the internet, I was told that the info message shouldn't interfere with the cron job itself
I am new to python and have pretty basic knowledge of Linux.
I need to start a script at boot time on a Ubuntu 14.04.3 server.
The only thing is, the script is a monitoring tool and should be running all the time so I can't just make a periodical cron call.
I found this at first : running a python script with cron
I have tried to add this in crontab :
#reboot python /path/to/script.py &
And also this :
#reboot /path/to/script.py &
but it doesn't seems to work.
I have also seen this : How to make a python script run like a service or daemon in linux
The main answer is cron or a change in the python code.
So my question is : Is there another way to run my script at boot and let it run "forever" without changing the code ?
I assure you if I don't want to change the code it's not by lazyness but I will if it's the only option.
Other information (don't know if it's necessary), I am running Windows and have access to the server via PuTTY.
The version of Python is 2.7
UPDATE
Here is the cron log :
Nov 27 15:57:03 trustyovh cron[760]: (CRON) INFO (pidfile fd = 3)
Nov 27 15:57:03 trustyovh cron[798]: (CRON) STARTUP (fork ok)
Nov 27 15:57:03 trustyovh cron[798]: (CRON) INFO (Running #reboot jobs)
Nov 27 15:57:03 trustyovh CRON[807]: (administrateur) CMD (/home/administrateur/scuMonitor/main.py &)
Nov 27 15:57:03 trustyovh CRON[800]: (CRON) info (No MTA installed, discarding output)
Nov 27 16:09:01 trustyovh CRON[1792]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))
Here is the crontab :
#reboot /home/administrateur/scuMonitor/main.py &
UPDATE 2
Well, it was actually working with the cron set to reboot, but, my script didn't put his log where I expected it to do (I wasn't understanding how the path work on Linux).
Thanks for all the answers everyone !
I would suggest you the same thing I have written here
Basically, you can run your python code as a service using systemd, all you have to do is to write a <your-app-name>.service file, like the one below
[Unit]
Description=Some kind of description
[Service]
Type=simple
ExecStart=<path to your bin with args if needed>
then, save it under /etc/systemd/system/. To check if everything is fine, run
sudo systemctl start <your-app-name>
and then
sudo systemctl status <your-app-name>
Finally run
sudo systemctl enable <your-app-name>
and the service will be executed at each system boot.
Taken from Run Python script at startup in Ubuntu.
You can start a service on Ubuntu by adding it to the /etc/init directory.
Put this in /etc/init
mystartupscript.conf
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py
As far as i know the only way to keep it checking for what ever you want it to check for is by implementing a loop in the code/daemonizing.
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