Cronjob Not Running

CronJob not running

Finally I found the solution. Following is the solution:-

  1. Never use relative path in python scripts to be executed via crontab.
    I did something like this instead:-

    import os
    import sys
    import time, datetime

    CLASS_PATH = '/srv/www/live/mainapp/classes'
    SETTINGS_PATH = '/srv/www/live/foodtrade'
    sys.path.insert(0, CLASS_PATH)
    sys.path.insert(1,SETTINGS_PATH)

    import other_py_files
  2. Never supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.

Cron Jobs Not Running at all, Working manually

The __DIR__ would give the script working directory so when using require it would always translate the path correctly

example:

<?php
define('DIR', __DIR__);
require_once(DIR.'../backend/backend.php');
?>

Django cronjob not running

Ok, I've found the solution.
The crontab was actually running (as I tought), because te log said so.

But I was using a executable in my python file, which crontab could not find. This had to do something with that crontab doesn't have PATH by default.
The user shell does look in path for the executable. Crontab doesn't.
You can fix this by adding at the top of your crontab:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I've appended this to my cronjob, to get the output of my python script:

 >> /home/user/cronlog.log 2>&1

Cronjob not running on Python and Mac

You can set up a cron job via crontab -e, which is much better than creating a python script to create a cron job. But anyways.

  1. You can first troubleshoot by actually running your python script and seeing if there are any errors, and if the file is even created.

  2. make sure the user for that cron job has the right permissions to execute the script.

  3. try executing the command: python /Users/myname/Desktop/customers/cronjob/testjob.py directly from your terminal.

Judging from your response, the reason why its not working is because your script isn't able to open the text file.

When you're executing the script via: python /Users/myname/Desktop/customers/cronjob/testjob.py, the location of your text file "testcron.txt" depends entirely on where you are executing the script from.

So basically, unless "testcron.txt" is located in the same path / directory from where you are executing the script, its not going to work.

You can fix this by changing your cron tab to first navigate to where your text file is, and then run the python script.

For example, if your "testcron.txt" file is located in /Users/myname/Desktop/customers/cronjob/ then write your cron job as:

cd /Users/myname/Desktop/customers/cronjob && python ./testjob.py

Crontab Linux not running at specific time

Your cron expression looks correct.
As it follows this format:

minute hour day-of-month month day-of-week [user] command
0 7 * * * /usr/bin/curl http://localhost//blablaba

PS: You need to add a user field if your are using the /etc/crontab file and not a user specific crontab.

So, if your cron job is defined in the /etc/crontab file, then you need to add a user field to your cron job to make it run. If it's not the case, then check your URL on the client and server side.

But, I think the best thing to do would be debugging the cron job. SO, you need to enable logging for cron jobs by doing the following:

If you are on Ubuntu:

sudo vi /etc/rsyslog.d/50-default.conf

If you are on CentOS:

sudo vi /etc/rsyslog.conf

Uncomment this line if it is commented:

#cron.*                         /var/log/cron.log

Then restart rsyslog and cron:

sudo service rsyslog restart
sudo service cron restart or sudo service crond restart

Now you can check the /var/log/cron.log file for log messages and see if there are any problems.



Related Topics



Leave a reply



Submit