How to run groovy script using crontab? - cron

I've tried to run a groovy script, using crontab:
16 21 * * * root groovy /home/vasily/test.groovy
But I have an error:
/bin/sh: 1: groovy: not found

You have to create a bash script, something like this
#!/bin/sh
JAVA_HOME=/usr/java/jdk1.6.0_12
GROOVY_HOME=/home/jboss/groovy-1.7.5
export JAVA_HOME
export GROOVY_HOME
PATH=$PATH:$JAVA_HOME/bin:$GROOVY_HOME/bin:
export PATH
cd /home/jboss/jboss-4.2.1.GA/server/flt/conf/scripts
(Assuming that Groovy script located at /home/jboss/jboss-4.2.1.GA/server/flt/conf/scripts)
groovy -classpath /home/jboss/jboss-4.2.1.GA/server/flt/conf/scripts /home/jboss/jboss-4.2.1.GA/server/flt/conf/scripts/test.groovy
and within the crontab you can call your bash script

Related

crontab does not execute script but it executable manually

Want to schedule a sh-script but crontab does not execute it.
However, the script is executable manually without any issues.
"crontab -l"-Output:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
48 16 * * 1,2,3,4,5 /home/pentaho_admin/pdi/schedule.sh
the shell script included a command with the following command in the 2nd line:
./kitchen.sh
1st line:
alias xyz="cd..." (leads to the folder of kitchen.sh)
That first line apparently cannot be interpreted by crontab.
Solution:
I've added the full path to kitchen.sh
Is there any more sexy solution?

crontab is returning error

I'm trying to run some crawler with Linux crontab.
This should go to the Python environment with
pyenv shell jake-crawler
Here is my crontab -e
*/10 * * * * /home/ammt/apps/crawler/scripts/bat_start.sh
This will run every 10 minutes. This command line works fine when I type
(jake-crawler) [jake#KIBA_OM crawler]$ /home/jake/apps/crawler/scripts/bat_start.sh
[DEBUG|run.py:30] 2017-09-24 19:55:49,980 > BATCH_SN:1, COLL_SN:1, 1955 equal 0908 = False
Inside of bat_start.sh I have init.sh which changes the environment to Python.
Here is my init.sh
#!/usr/bin/env bash
export PATH="${HOME}/.pyenv/scripts:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
pyenv shell jake-crawler
This has no problem when I personally run it from command line. But when cron run it by itself, it cannot find the pyenv command.
I think that you can specify which user should run that script in cron configuration file.
So, if that script is working with your user, then define it in your cron configuration filr.
See this answer for example... https://stackoverflow.com/a/8475757/3827004.
There are two things that diferentiate when you launch an application from the terminal, and when you do from a crontab file:
the environment is not the same, at least if you don't execute your .profile script from your cron job.
You don't have access to a terminal. Cron jobs don't use a terminal, so you will not be able, for example to open /dev/tty. You will have to be very careful on how redirections are handled, as you have them all directed to your tty when running on an interactive session, but all of them will be redirected possibly to a pipe, when run from cron(8).
This makes your environment quite different and is normally a source of errors. Read crontab(1) man page for details.

Trying to run script through crontab which won't work

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.
Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com
An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

Ubuntu cron shebang not working

I have a script with this as the shebang #!/usr/bin/env node.
When cron runs my script, I get this error /usr/bin/env: node: No such file or directory.
When i run the script as my user the script runs fine, just not as cron. I'm guessing it's because node is not on the PATH of the user that runs cron?
How can I get this shebang to work with cron?
$ which node gives me
/home/myuser/.nvm/v0.11.14/bin/node
Cron jobs run in a restricted environment. In an interactive shell, your $PATH is probably set in your $HOME/.bash_profile or $HOME/.bashrc. Cron jobs are executed in an environment that hasn't sourced those files, so your user-specific $PATH settings will not be available.
You can see what that environment looks like by temporarily creating a crontab entry like:
* * * * * printenv > crontab-environment
You can explicitly set $PATH in your crontab, either in the command itself:
* * * * * PATH=$PATH:/home/myuser/.nvm/v0.11.14/bin some_command
or in a separate line in your crontab:
PATH = /usr/bin:/bin:/home/myuser/.nvm/v0.11.14/bin
You can't (directly) use the usual PATH=$PATH:/new/dir syntax to append a directory to your $PATH in an environment setting line, because variable references are not replaced in such a line. They are processed in crontab command lines.
man 5 crontab for details.
Another option is to use an explicit full path in the script itself, changing
#!/usr/bin/env node
to
#!/home/myuser/.nvm/v0.11.14/bin/node
You'll need to customize this for each system where node is installed in a different place. Or you can arrange for node (or nodejs?) to be installed in a consistent place.

Crontab fails to run my Perl module

My perl module is working fine when whenever I run it without using crontab,
but when I try running the script through Crontab it's throwing some error like couldnt find other modules which have been called inside my Perl.pl file.
The line I have added in Crontab file is
* * * * * cd /usr/bin/perl | perl path_to_script/script.pl;
The error I am getting is
Can't locate module.pm in #INC (#INC contains: /home/path/lib /usr/another/path/lib /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at path_to_script/script.pl line 23
When ever I am tring to run the script through crontab file it fails to connect to the sub modules.
So many things can be different when running from crontab. Such as:
different user
different shell
Often the $PATH under crontab is not what you usually expect it to be. You may have to explicitly provide the $PERL5LIB environment variable so a Perl script knows what library folders to look in.
try this :
*/5 * * * * bash -l perl path/script.pl
# change the above to your crontab settings
Run Perl with an -I (uppercase i) option indicating where to find the missing module(s), or create a wrapper something like
#!/bin/sh
cd /usr/lib/perl
PERL5LIB=$HOME/lib/perl perl path_to_script/script.pl
... and run this script from your crontab instead.
(The value of PERL5LIB might have to be more complex than this; maybe copy/paste from its value in your interactive shell.)
Just add the PERL5LIB environment variable in /etc/crontab and include the path to the .pm module file.
Example:
PERL5LIB=/root/perl5/lib/perl5:/usr/share/perl5

Resources