Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Ignacio_Garcia
Participant

Calling a Bash Script in Cron

Hello

I'm trying to scheddule a cronjob (bash script) in our management station. Such bash script, when executed manually, works flawlesly, but when scheduled over cron, it does not work. sk77300 and sk90441 have not helped at all.

Any other ideas?

16 Replies
Vladimir
Champion
Champion

1. Please check if cron daemon is running.

2. Please check if your script ends with the newline.

0 Kudos
PhoneBoy
Admin
Admin

Keep in mind some environment variables in the script may not be defined in the environment that runs the cron script.

It would be helpful if you could provide the errors you're getting.

Ignacio_Garcia
Participant

Hi Dameon

No errors at all in /var/log/cron. any other file i should be looking at?

Regards

0 Kudos
Timothy_Hall
Champion
Champion

Are you explicitly declaring the command interpreter (i.e. magic number) at the start of the shell script like this:

#!/bin/bash -f

If so, try adding -x like this:

#!/bin/bash -f -x

-x causes every command being executed by the script and its arguments to be output as it happens.  Very helpful for troubleshooting.  Also make sure that any commands being invoked in the script are using absolute paths (i.e. /bin/ls) and try to avoid using relative paths and paths referencing an environment variable such as $FWDIR/bin/ until you figure out what is wrong.

--
My book "Max Power: Check Point Firewall Performance Optimization"
now available via http://maxpowerfirewalls.com.

Gateway Performance Optimization R81.20 Course
now available at maxpowerfirewalls.com
Luis_Miguel_Mig
Advisor

If you need  to load the environment variables:  "source /etc/profile.d/CP.sh"

0 Kudos
Ignacio_Garcia
Participant

Hi Luis

The line is in the script.

@Tim: Those options are not recognized:

/bin/bash: - : invalid option

I had $MDSVERUTIL in my script, which i exchanged with /opt/CPmds-R77/system/shared/MDSVerUtil.  Situation is still the same when script is run over Cron (which is active), and running it manually gives no problem

0 Kudos
Luis_Miguel_Mig
Advisor

What errors do you get? Make sure you sent the output of the script execution to a file where you can see the errors

0 Kudos
Sergio_Muniesa
Explorer

Not sure if this helps... Be sure to write the full path of all commands into cron and/or script invoked.

Refer to:

difference between cron and command line environment? shell script errors 

0 Kudos
Ignacio_Garcia
Participant

The below produces no out put at all:

*/5 * * * * /home/indeni/wf_session_check.sh >> /home/indeni/session.txt 2>&1

0 Kudos
Hugo_vd_Kooij
Advisor

As which user are you adding this crontab job?

Does that use have sufficient access rights?

Keep in minds we have zero knowledge of what you have done or what your setup looks like. Unless you tell us we simply don't know.

<< We make miracles happen while you wait. The impossible jobs take just a wee bit longer. >>
0 Kudos
Hugo_vd_Kooij
Advisor

Can you show us the script?

What "source" line do you use?

<< We make miracles happen while you wait. The impossible jobs take just a wee bit longer. >>
Luis_Miguel_Mig
Advisor

If you see nothing at /home/indeni/session.txt it is probably because cron is not running your script.

0 Kudos
Maarten_Sjouw
Champion
Champion

This is how we run scripts from cron, in clish add the following command:

add cron job MDS_backup command "/backup/scripts/FWBackupMDS >/dev/null 2>&1" recurrence daily time 1:00

The  >/dev/null 2>&1 makes sure the output is redirected properly. On the first line of the script put this line:

#!/bin/sh

After that all other things you need to do.

<crontab -l> shows me:

# This file was AUTOMATICALLY GENERATED
# Generated by /bin/cron_xlate on Mon Aug 7 08:42:47 2017
#
# DO NOT EDIT
#
SHELL=/bin/bash
MAILTO=""
#
# mins hrs daysinm months daysinw command
#

##MDS_backup
00 1 * * * /backup/scripts/FWBackupMDS >/dev/null 2>&1

Regards, Maarten
0 Kudos
007_mjn
Contributor

Hi @Maarten_Sjouw 

How to add crontab job for interface link status down or up and send mail if link is down.

0 Kudos
Henning_Gunders
Participant

@Ignacio Garcia: I am not sure if the problem was resolved?
When a script doesn't run as a cronjob, then the reason is almost always one of three things:
1) The cronjob user does not have the Bash shell set as default, or
2) the CP environment parameters is not loading correctly.
3)
On rare occations I have experienced that one must run the command "dos2unix *.sh" to get the cronjob working.

I recently discovered that sk77300 is no longer correct.
Changing the <version> parameter to R80 only works with R80 and R80.10
source /opt/CPshrd-R80/tmp/.CPprofile.sh

When using R80.20 it must look like this:
source /opt/CPshrd-R80.20/tmp/.CPprofile.sh

 

Hope this helps! Smiley Happy

Matt_Taber
Contributor

I found that if you add the -l flag to your shebang:

#!/bin/bash -l

Things behave w/in crontab in a more expected fashion.

From 'man bash':

-l Make bash act as if it had been invoked as a login shell (see INVOCATION below)

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events