How to Perform a Specific Task in cron Immediately

Asked 1 years ago, Updated 1 years ago, 105 views

using bash-c to create job behavior for cron
3012**1-5bash-c'export PATH="/usr/local/bin:/opt/rbenv/bin:$PATH";eval "$(rbenvinit-)"; cd/opt/foo;/opt/rbenv/shims/ruby foo.rb'

You can set and execute the necessary environment variables, such as in , but they may be different from the environment on the shell and result in errors.

In such cases, I always set up a task after a minute to see how it works, but is there a way to immediately perform a specific task from the crontab list without waiting for 1-59 seconds?

The environment uses Ubuntu 14.04.

linux cron

2022-09-30 20:23

4 Answers

Debian/Ubuntu comes standard with a package called faketime, which allows you to fool the time and date of the process running under faketime.

$sudo apt-get install failtime
$ failtime-f x10 bash-c'while:; do sleep1; date; done'

I tried to launch the cron that I had on hand through faketime.
Unfortunately, I couldn't fool you.

This is not an immediate way to run a cron job, but if you want to run a cron job using the environment configured by bash's rc, try giving bash the -l option to start it as a login shell.

3012**1-5bash-lc'export PATH="/usr/local/bin:/opt/rbenv/bin:$PATH";eval "$(rbenvinit-)"; cd/opt/foo;/opt/rbenv/shims/ruby foo.rb'

By the way, if you run multiple commands on a shell and the subsequent commands depend on the results of the previous command, it is safer to separate the commands with & instead of ;.This allows you to abort when the command fails.

3012**1-5bash-lc'export PATH="/usr/local/bin:/opt/rbenv/bin:$PATH";eval"$(rbenvinit-)"&cd/opt/foo&&/opt/rbenv/shims/ruby foo.rb'rb'rb'

Alternatively, you should run set-e first.

3012**1-5bash-lc'set-e; export PATH="/usr/local/bin:/opt/rbenv/bin:$PATH";eval"$(rbenvinit-); cd/opt/foo;/opt/rbenv/shims/ruby foo.rb'


2022-09-30 20:23

Cron reads and evaluates the configuration file once a minute, so it's hard to keep it cron.


Shell scripts with infinite loops with sleep in between, if you just want to run them at regular intervals. It might be easier to register with rc.d.

If you do not start using the service command, the environment variable will get caught in a different trap, so be careful.
In the case of FreeBSD, there is no problem, but it seems that some environmental variables remain in the Linux system.
http://heartbeats.jp/hbblog/2013/06/service-start-stop.html

Service management software also supports infinite loops such as runit and djb daemontools.


2022-09-30 20:23

UNIX-based OSes have a command called at that allows you to execute the command only once with a specified time.
Internally, it should be queuing in the same way as cron, so why don't you try using this?

I'd like you to see man at for more information, but I should be able to say at now immediately.


2022-09-30 20:23

Actually, crontab can write the environment variable settings on the top line of the time information as follows.

FOO=BAR
3012**1-5/path/fo/foo

Even if you copy it and run it, the results can be different.
On the Internet, some are extracted from the results of crontab-l using grep and sed, but for the above reasons, it is not reliable.

The best solution is...

I think the best solution is to write all the execution contents including setting environment variables in the script and use only the execution statements in crontab.

add

If you want the environment variables to be the same

Running a cron job manually and immediately

As you said here, I think it's better to spit out env and check it out.


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.