0

im working with sun Solaris 10 which legacy system, I have made a script that handles specific producer and it consists of bash/perl/posix/sqlplus/awk around 1k lines and it works fine in standalone mode it would work as expected, but there was an issue where the script does not work in crontab I have followed a lot of suggestion through StackOverflow/stackunixexchange like:

  1. start with shebang #!/bin/bash
  2. script with full permission 777
  3. right crontab time syntax
  4. service restarted
  5. loaded all environment variables in the top of the shell
  6. set bash at the beginning of the script in crontab bash

my issue only on that script and the rest of scripts works fine maybe because the script has to be in bash and crontab runs by default with sh or crontab load minimal environment variable

finally, I have solved the issue by inserting the needed shell into another shell and run it through the new shell by setting the new shell in crontab, after this step I can see the crontab worked fine and it can run the script but the issue the script does not do the expected job, it skips bash statements like if statements and other jobs like sqlplus producers /Perl statements can you please tell how can solve this issue, this problem happened only when scheduling the script into crontab

shell in crontab looks like :

10 10,16 * * 1,4 bash /rate/u01/WORK/MP/ERROR/USAGE_REJECTIONS/HolderUsageShell.sh

the script starts as below :

#!/bin/bash 
#environment shell contains all environment variables, I have tried to run it with/without and I got the same thing
/rate/environment.sh >/dev/null

cd /rate/path/
CurrentHour=$(date +%I)
DAY_In_Word=$(date +%A)

if [[ "$DAY_In_Word" == "Monday" || "$DAY_In_Word" == "Thursday" ]] && [[ "$CurrentHour" == "10" ]]; then
.
.
.
  • It might help to share the script, or a sample with an couple of statements that don't work. – Gerard H. Pille Apr 7 at 10:43
  • the script work fine in standalone, the problem only when i run the script in crontab or set the script inside another script that scheduled in crontab – Nikolan Asad Apr 7 at 11:59
  • Yes, you said so in the original question. I believe you. – Gerard H. Pille Apr 7 at 12:24
  • @GerardH.Pille i have updated my question dear with crontab & script sample – Nikolan Asad Apr 7 at 20:12
0

You cannot change your environment in a subshell: when the subshell ends, changes are lost. The subshell cannot change the parent's environment. You must change the environment in the current shell. Make this change to your script:

/rate/environment.sh >/dev/null

should be

. /rate/environment.sh >/dev/null
| improve this answer | |
  • thank you very much!, like a charm – Nikolan Asad Apr 8 at 10:19
  • Watch out with the ">/dev/null", useful information may get lost. Good luck! – Gerard H. Pille Apr 8 at 11:05

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.