I have a cron job that is scheduled to run everyday, other than changing the schedule, is there any other way to do a test run of the command right now to see if it works as intended?
As far as i know there is no way to directly do that as cron has a special purpose - running schedules commands at a specific time. So the best thing is to either to manually create a crontab entry or write a script which removes and resets the environment.
You can force the crontab to run with following command:
You can simulate the cron user environment as explained in "Running a cron job manually and immediately". This will allow you to test the job works when it would be run as the cron user.
Excerpt from link:
Step 1: I put this line temporarily in the user's crontab:
* * * * * /usr/bin/env > /home/username/tmp/cron-env
then took it out once the file was written.
Step 2: Made myself a little run-as-cron bash script containing:
#!/bin/bash /usr/bin/env -i $(cat /home/username/tmp/cron-env) "$@"
So then, as the user in question, I was able to
run-as-cron /the/problematic/script --with arguments --and parameters
CronitorCLI has a command
cronitor select that lets you select and run any cron job from the command line. You do not need to create a Cronitor account to use it.
Here is an example:
ubuntu@ip-10-0-0-112:~$ cronitor select Use the arrow keys to navigate: ↓ ↑ ? Select job to run: ▸ /var/runner/src/bin/batch_reports.py runner.settings.prod /var/runner/src/bin/trigger_reports.py runner.settings.prod ... etc ...
I found a solution that seems to be a little better for my purposes (commands shown for CentOS / RHEL-like, but should be adaptable basically anywhere).
- Stop crond service -
service crond stop
- Figure out when your service should run by - https://crontab.guru is pretty useful for this.
- Run crond in foreground mode through libfaketime's
faketimetool (it lets you fake out the syscall for time lookups for any child processes).
- I wouldn't run this on a production server
faketime '2019-10-17 07:59:50' /usr/sbin/crond -n -x test,sch
[root@user-crontesting-dvc-01 ~]# faketime '2019-10-17 07:59:50' /usr/sbin/crond -n -x sch debug flags enabled: sch  cron started log_it: (CRON 4841) INFO (Syslog will be used instead of sendmail.) log_it: (CRON 4841) INFO (RANDOM_DELAY will be scaled with factor 34% if used.) log_it: (CRON 4841) INFO (running with inotify support)  GMToff=0 log_it: (CRON 4841) INFO (@reboot jobs will be run at computer's startup.)  Target time=1571299200, sec-to-wait=11 user [root:0:0:...] cmd="/usr/libexec/myexc/crontesting.cron > /dev/null 2> &1"  Target time=1571299260, sec-to-wait=60 log_it: (root 4844) CMD (/usr/libexec/myexc/crontesting.cron > /dev/null 2> &1) log_it: (root 4843) CMDOUT (/bin/bash: -c: line 0: syntax error near unexpected token `&') log_it: (root 4843) CMDOUT (/bin/bash: -c: line 0: `/usr/libexec/myexc/crontesting.cron > /dev/null 2> &1')