0

I have a shell script where I am calling the hana.scr script from within the main script. The hana.scr contains the below code.

chmod 777 /data/auto/SLT.out; rm -rf /data/auto/SLT.out; hdbsql -n plhesappr61 -i 00 -u USR -p $#^F#$GGG -o /data/auto/SLT.out "Select sum("ERPACC_RPPCLNT200"."VABD"."NETWR") FROM "ACC_CLNT"."VFKH" inner join "ACC_CLNT"."VNRO" on ("ACC_CLNT"."VNRO"."VBELN"="ACC_CLNT"."VFKH"."VBELN") where FKART in ('ZFP1','ZFP3') and FKDAT = (select ADD_DAYS (TO_DATE (current_date, 'YYYY-MM-DD'), -1) "add_days" from dummy) group by FKDAT";

When I run the main script manually, it calls this script fine and the SLT.out file is also generated. But when I schedule it in cron, the main script executes just fine, except for this hana.scr which does not seem to execute because it does not does not even remove the old file as per the second command rm in the hana.scr.

The cron is the same user as the one I run the script manually with. I read that if the cron does not get the same environment to run, these issues happen. I tried to import the UNIX profile of the user before executing as the hana.scr as well, but was not successful.

Below is the cron command which runs the main script which calls the hana.scr from within: Used absolute paths..

37 0,2,3,4,5,6 * * * /data/esb/auto/./main.sh R > /data/esb/auto/main.log

The hana.scr is executed in the below manner:

./hana.scr;
check6=$? ;
                                if [ $check6 = "1" ]
                                        then
                                                echo "***********HANA counts were not generated**********"
                                fi
user3055262
  • 405
  • 3
  • 9
  • 20
  • Redirect the cron job's error as well to main.log and see what happens. – codeforester Dec 23 '16 at 18:45
  • 2
    Inside "main.sh" is there some condition based on which "hana.scr" is launched? how exactly the "hana.scr" is launched? Could there be any reason that "hana.scr" is not executed because "main.sh" fails earlier then "hana.scr" would be started ? – czvtools Dec 23 '16 at 21:46
  • the hana.scr is launched in the below manner: ./hana.scr; check6=$? ; if [ $check6 = "1" ] then echo "***********HANA counts were not generated**********" fi – user3055262 Jan 02 '17 at 07:43
  • Probably it is a PATH issue. Try to call hana.scr using abosolute pathname. Note aside: why chmod 777 a file just before deleting it? To remove a file you need write permission in its directory, not the file itself. – linuxfan says Reinstate Monica Jan 02 '17 at 08:31
  • Changed to absolute path, still nothing. The $? after that also returns 0(complete code is present above). That indicates that this ran fine, but in reality it does not run at all. If it ran, it would have created a new SLT.out file. For chmod, your point noted :) – user3055262 Jan 03 '17 at 14:32

1 Answers1

1

After /data/esb/auto/./main.sh your current directory is not changed to /data/esb/auto/. I think you started main.sh from the commandline while your $PWD was the same as where hana.scr was. Test it from the commandline with

cd /
/data/esb/auto/main.sh

How to fix? The worst solution is changing the crontab line into

37 0,2,3,4,5,6 * * * cd /data/esb/auto; /data/esb/auto/main.sh R > /data/esb/auto/main.log

That is a workaround for the crontab but main.sh still fails when started from a different directory.
Slightly better is using the complete path in main.sh when you call hana.scr

myscriptdir=/data/esb/auto
..
${myscriptdir}/hana.scr

When you change the folders you need to edit the files and repair the settings.
You can try to use some config file with settings or let main.sh figure out what in which directory it is:
Getting the source directory of a Bash script from within

Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43