![Whmcs manually run cronus Whmcs manually run cronus](/uploads/1/2/5/6/125617492/532421439.png)
I have a cron job running for each night at 12 ish but if i ever want to do this manually one time for example - if i manually add 5 clients who are. I have my Cron to run Daily at 1am, But this there a way to manually run it? I would like to run manually a couple times to run some tests.
(I have already read.) I have a specific problem (cron job doesn't appear to run, or run properly), but the issue is general: I'd like to debug scripts that are cronned. I am aware that I can set up a. crontab line, but that is not a fully satisfactory solution. I would like to be able to run a cron job from the command line as if cron were running it (same user, same environment variables, etc.). Is there a way to do this? Having to wait 60 seconds to test script changes is not practical.
Here's what I did, and it seems to work in this situation. At least, it shows me an error, whereas running from the command line as the user doesn't show the error. 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 This solution could obviously be expanded to make use of sudo or such for more flexibility. Hope this helps others.
This does not work for me and I wonder if it does for anybody who upvoted. 1) Why are you using bash? It's not required here and it might not be located in /usr/bin. 2) The cat /cron-env outputs multiple lines, which is does not work. Just try to execute /usr/bin/env -i $(cat cron-env) echo $PATH in the terminal, it outputs the environment literally instead of using it. 3) The current environment leaks into the emulated cron environment.
Try: export foo=leaked; run-as-cron echo $foo. – Sep 24 '13 at 18:33. Cat outputs multiple lines, which do work, because shell substitution collapses them into a single line, which you can check with echo $(cat cron-env ) wc; your example command, /usr/bin/env -i $(cat cron-env) echo $PATH, substitutes $PATH from the calling shell; instead, it should invoke a subshell to substitute in the subenvironement, e.g. /usr/bin/env -i $(cat cron-env) /bin/sh -c 'echo $PATH'.
You've made the same mistake, again substituting in the calling shell instead of in the subenvironment – Jun 21 '16 at 17:17. I present a solution based on Pistos answer, but without the flaws. Add the following line to the crontab, e.g.
Using crontab -e. /usr/bin/env /home/username/cron-env.
Create a shell script which executes a command in the same environment as cron jobs run: #!/bin/sh. '$1' exec /usr/bin/env -i '$SHELL' -c '.
$1; $2' Use: run-as-cron e.g. Run-as-cron /home/username/cron-env 'echo $PATH' Note that the second argument needs to be quoted if it requires an argument. The first line of the script loads a POSIX shell as interpreter. The second line sources the cron environment file. This is required to load the correct shell, which is stored in the environment variable SHELL. Then it loads an empty environment (to prevent leaking of environment variables into the new shell), launches the same shell which is used for cronjobs and loads the cron environment variables.
Finally the command is executed. By default with most default cron daemons that I have seen, there is simply no way of telling cron to run right here right now.
If you're using anacron, it may be possible I think to run a separate instance in the foreground. If your scripts aren't running properly then you are not taking into account that. the script is running as a particular user. cron has a restricted environment (the most obvious manifestation of this is a different path).
From crontab(5): Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab’s owner. PATH is set to '/usr/bin:/bin'. HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is running from, and may not be changed. In general PATH is the biggest problem, so you need to:. Explicitly set the PATH within the script, while testing, to /usr/bin:/bin.
You can do this in bash with export PATH='/usr/bin:/bin'. Explicitly set the proper PATH you want at the top of the crontab. PATH='/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin' If you need to run the script as another user without a shell (e.g. Www-data), use sudo: sudo -u www-data /path/to/crontab-script.sh The first thing to test before all of that, of course, is that your script actually does what it is supposed to do from the command line. If you can't run it from the command line, it will obviously not work from with cron.