4

The cron job for updatedb didn't run because the system was powered off at the time. I'd like to tell cron to run that job now, as if now were the schedule time. Is there a way to do that?

For this job I don't want to run it in the foreground because I don't want to keep it open, and I don't want to run it in a screen because I don't want to have to clean it up later. On many other occasions it would have been useful to do the same thing to test that a job would execute correctly in the environment provided by cron.

2

The environment used by cron is defined in /etc/crontab

You can run your command using that file to set up the environment, for example:

env - `cat /etc/crontab | grep -v "#" | grep -v "^$" | grep -v /etc/cron` your_script_name.sh

This strips the comments (lines starting with #), blank lines (matching regex: ^$), and the system-wide crontab entries themselves, leaving only the environment variables. Those variables are used to run your script in a modified environment via the "env" command.

If you want to background the process you can do that with nohup:

nohup 'env - `cat /etc/crontab | grep -v "#" | grep -v "^$" | grep -v /etc/cron` your_script_name.sh' &

Solution taken from: http://benohead.com/linux-simulate-the-cron-environment-to-test-run-your-scripts/

1

What about

nohup updatedb &

executed in a terminal (xterm or console)?

0

I created a Bash script once with logic for manual runs for testing and known missed runs. It utilized simple file existence to work, kind of the reverse of a LOCK file.

To make it work the script consisted of 2 sections or routines. Our cron was designed to run every twelve hours at noon or midnite, however you can apply this logic to any timetable.

Section A of the code, checked for Noon or midnight and if it was, touched a file: touch ./RUNCRON

Section B of the code, checked for the existence of ./RUNCRON. The code of our original cron job was then inserted as the THEN body of the IF, as a last statement we removed the file ./RUNCRON

The new cron job was set to execute every minute instead of every 12 hours. To trigger a manual run all we had to do was touch ~/path/to/RUNCRON

0

You can use at to run commands non-interactively:

at now + 1 min
at> /command/to/run
at> ^D
job 2 at Wed Jul 30 14:27:00 2014

"now + 1 min" means to run the command one minute in the future. at will prompt you for the command(s) to run; hit Cntrl-D when you're finished. at will run the command(s) in the same current directory and with the same environment variables as for your interactive session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.