- Verify if crontab works
- 9 Answers 9
- How to check cron syntax by script like "crontab -e" does?
- 2 Answers 2
- How to view a cron job running currently?
- 6 Answers 6
- How to List, Display, & View all Current Cron Jobs in Linux
- Listing Cron Jobs in Linux
- How to List all Active Cron Jobs Running
- View Cron Jobs by User
- How to List Hourly Cron Jobs
- How to List Daily Cron Jobs
- How to Display Weekly Cron Jobs
- How to List Monthly Cron Jobs
- View Software Specific Cron Jobs
Verify if crontab works
I know that i redirect to the null device, but i not sure if the above command is good. *Edit 1: In my /var/log/syslog every two minutes i have the following error:
(CRON) error (grandchild #2788 failed with exit status 2)
*/2 * * * * /usr/bin/php /path_to_my_php_script/info.php >/dev/null 2>&1
unfortunately all above tried but does not work for me. as you say the trial /bin/sh -c «(export PATH=/bin; /tmp/trial.sh
9 Answers 9
Although very rare, sometimes cron stops working properly even though the service is running. Here is how to verify that crond is running and stop/start the service.
Using systemctl:
systemctl status cron systemctl restart cron
Older methods:
On CentOS and other Red Hat-based systems:
service crond status service crond stop service crond start
On Ubuntu and other Debian-based systems:
service cron status service cron stop service cron start
@mpen service is for systemd (which was adopted by many Linux distributions). Older releases and some distributions may still be using the old System V scripts in /etc/init.d .
The syntax for the crontab entry looks correct. Indeed, if you edit your crontab using » crontab -e » (as you should), you’ll get an error if you specify a syntactically invalid crontab entry anyway.
- Firstly, does /path_to_my_php_script/info.php run correctly from the command-line?
- If so, does it also run correctly like this?:
/bin/sh -c "(export PATH=/usr/bin:/bin; /path_to_my_php_script/info.php
/bin/sh -c "(export PATH=/usr/bin:/bin; /path_to_my_php_script/info.php /dev/null 2>&1)"
Step (3) is similar to how cron will run your program (as documented in "man 5 cron".
The most likely problem you're having is that the PATH cron is using to run your program is too restrictive. Therefore, you may wish to add something like the following to the top of your crontab entry (you'll need to add in whatever directories your script will need):
Also note that cron will by default use /bin/sh , not bash. If you need bash, also add this to the start of your crontab file:
Note that both those changes will affect all the crontab entries. If you just want to modify these values for your info.php program, you could do something like this:
*/2 * * * * /bin/bash -c ". ~/.bashrc; /path_to_my_php_script/info.php"
It's also worth mentioning that on a system configured for "mail" (in other words a system which has an MTA configured [sendmail/postfix/etc]), all output from crontab programs is sent to you via email automatically. A default Ubuntu desktop system won't have local mail configured, but if you're working on a server you can just type "mail" in a terminal to see all those cron mails. This also applies to the " at " command.
How to check cron syntax by script like "crontab -e" does?
Let's say I write cron files via script and modify them direct under /var/spool/cron/crontabs/ . When using the command crontab -e crontab checks the syntax when I exit the editor. Is there any way to do the same check via script?
2 Answers 2
Crontab with -e option does open your default editor with the current cron file and installs it after exiting.
First of all, save your actual cron file to be sure of not losing or breaking anything.
You can directly install a file that you have cooked:
Or use a pipe in a script:
#/bin/bash Variable="your scheduled tasks" echo $Variable | crontab
You will get the error messages in the case of bad formatting.
Keep in mind that this will delete any previous crontab there was before, check it with crontab -l before
@golimar yes, all protection is good when you touch this kind of configuration files. I update my answer with your suggestion.
chkcrontab /etc/cron.d/power-schedule Checking correctness of /etc/cron.d/power-schedule E: 15: 0 12 * foo * * root echo hi e: FIELD_VALUE_ERROR: foo is not valid for field "month" (foo) e: INVALID_USER: Invalid username "*" E: There were 2 errors and 0 warnings.
Or try a shell script 48-verifycron from Wicked Cool Shell Scripts, 2nd Edition, but it's not as good.
i started managing my cron-jobs with git and "chkcrontab" helps me inside my bash script to check if files are valid before pushing them into crontab, it works perfectly!
How to view a cron job running currently?
It is been set for daily execution. I usually open my machine around 8a.m. I would like to find out
- what time my cron job ran, if it has already run ?
- I would also like to see if any of my cron job is running at the moment? Is there a way to find out IF a cron job is actually running at the moment?
6 Answers 6
to check if cron is actually running anything at this moment in time (works on ubuntu)
2775,cron # your pid (2775) will be different to mine :-)
or a tree output with all the child processes that cron is running (it may not name them if you don't have sufficient privileges) and as Hamoriz says the logs are in /var/log/syslog so
will get you the logs just for cron
I would also like to see if any of my cron job is running at the moment?
what time my cron job ran ?
Cron log only show when start task off crond, not log when end. You need put this on your task or embedded your task en one scritp with control time of start and end.
cat /path/logs/messages or /path/logs/file when your system put logs of crond (this depends on your distribution settings or your computer)
This will show the executed grep command as part of the result, too. You could get rid of it e.g. by using ps aux | grep -v "grep" | grep "path/exec.sh" .
you can display all active cron jobs using
To vie cron job history, you can show logs by-
You can find cron demon is running or not by-
or all about cron status by-
If you are just interested in knowing if your cronjob is currently running and when it last started, then I find the following the easiest way:
0 0 * * * touch /path/cron.start; /path/exec.sh; touch /path/cron.end
This will create a file /path/cron.start with a timestamp which is the start time. When the job finishes, the file /path/cron.end will have the timestamp when the cron finished. So a simple ls -lrt /path/cron. will tell you when the job started and if it is still running (the order will tell you if it is still running).
How to List, Display, & View all Current Cron Jobs in Linux
Cron is a Linux utility for scheduling scripts and commands. This guide will show you several options to view current cron jobs scheduled in the crontab list.
Listing Cron Jobs in Linux
How to List all Active Cron Jobs Running
To list all scheduled cron jobs for the current user, enter:
Cron jobs are typically located in the spool directories. They are stored in tables called crontabs. You can find them in /var/spool/cron/crontabs. The tables contain the cron jobs for all users, except the root user.
The root user can use the crontab for the whole system.
To display contents of the root user’s crontab, use the less command:
The system returns an output like the following:
# /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
The /etc/crontab file can be edited using a text editor like nano:
In RedHat-based systems, this file is located at /etc/cron.d.
The /etc/ directory has additional cron subdirectories to organize hourly, daily, weekly, and monthly tasks. The ls (list) command displays files and directories. Use the -la option to list all entries in long format.
View Cron Jobs by User
To list cron jobs that belong to a specific user, run the following command:
sudo crontab -u [username] -l
Replace [username] with the actual username you're viewing.
How to List Hourly Cron Jobs
To list hourly cron jobs enter the following in the terminal window:
The output should appear similar to this:
How to List Daily Cron Jobs
To list daily cron jobs, enter the command:
The results will look similar to the following output:
How to Display Weekly Cron Jobs
To display weekly cron jobs:
The results will look something like the following:
total 28 drwxr-xr-x 2 root root 4096 Apr 24 20:46 . drwxr-xr-x 96 root root 4096 May 19 17:12 .. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder -rwxr-xr-x 1 root root 730 Feb 23 2014 apt-xapian-index -rwxr-xr-x 1 root root 427 Apr 16 2014 fstrim -rwxr-xr-x 1 root root 771 Sep 23 2014 man-db -rwxr-xr-x 1 root root 211 Mar 27 2017 update-notifier-common
How to List Monthly Cron Jobs
To display monthly cron jobs use the ls command in this format:
total 12 drwxr-xr-x 2 root root 4096 Apr 24 20:44 . drwxr-xr-x 96 root root 4096 May 19 17:12 .. -rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
View Software Specific Cron Jobs
To view software specefic cron tasks, start by viewing a list of cron tasks:
Use the cat command to display the contents of update-notifier-common :
The results will look similar to:
#!/bin/sh set -e [ -x /usr/lib/update-notifier/package-data-downloader ] || exit 0 # Try to rerun any package data downloads that failed at package install time. /usr/lib/update-notifier/package-data-downloader
Note: You should also learn more about Linux at command, another useful tool for scheduling jobs.
Now you know how to navigate through the cron jobs on your machine. Cron is a helpful utility for scheduling tasks such as running a job at reboot. Use the commands from this guide to sort and display tasks scheduled through the cron tool.