Unable to find SqlPlus processes running when exec

2019-06-10 01:41发布

问题:

I have a shell script that's being executed via Crontab. The shell script is properly creating the Sqlplus jobs and they run to completion. What's not working is the while loop at the end where I want the script to wait for all of the Sqlplus jobs to complete.

If I execute this shell script manually, the while loop at the end works properly and the shell script won't exit until all the Sqlplus jobs have finished.

How do I get the while loop at the end to see the Sqlplus jobs while running via Crontab?

#!/bin/bash
cd /some/path/to/folder

source ~/.profile

echo 'Run started'
echo $(date)

i=0
while [ $i -lt 12 ]
do
    echo 'Starting process ' $i

    sqlplus username/password@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server)(PORT=1521))(CONNECT_DATA=(SID=SERVERSID)))' @import.sql $i > import_batch$i.log &

    let i=$i+1
done

while [ $(ps -a | grep -w -c 'sqlplus') -ne 0 ] //This is not working in Crontab
until [[ -z $(pgrep -flx 'sqlplus') ]] //I've also tried this (instead of the while in my script) without success in Crontab
do
    sleep 60
done

echo 'Run completed'
echo $(date)

回答1:

As per my comment above, use "wait" to wait for all processes owned by this process to comlete. e.g.:

#!/bin/bash
cd /some/path/to/folder

source ~/.profile

echo 'Run started'
echo $(date)

i=0
while [ $i -lt 12 ]
do
    echo 'Starting process ' $i

    sqlplus username/password@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server)(PORT=1521))(CONNECT_DATA=(SID=SERVERSID)))' @import.sql $i > import_batch$i.log &

    let i=$i+1
done

wait

echo 'Run completed'
echo $(date)


回答2:

Try using

ps -axww | grep -w 'sqlplus' | grep -v grep | wc -l

because ps -axww | grep -w -c 'sqlplus' will always include 1 line for grep -w -c 'sqlplus' command

Alternatively, try using following to match sqlplus pattern exactly or as regex

pgrep -fl 'sqlplus'
pgrep -flx '.*sqlplus.*'


回答3:

Maybe you need to use ps -ax in the crontab case ?

while [ $(ps -ax | grep -w -c 'sqlplus') -ne 0 ]

EDIT 2013-04-27 : scratch that, that's dumb. As linuts suggested, just use wait.

#!/bin/bash
cd /some/path/to/folder

source ~/.profile

echo 'Run started'
echo $(date)

i=0
while [ $i -lt 12 ]
do
    echo 'Starting process ' $i

    sqlplus username/password@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server)(PORT=1521))(CONNECT_DATA=(SID=SERVERSID)))' @import.sql $i > import_batch$i.log &

    let i=$i+1
done

wait
echo 'Run completed'
echo $(date)