Unable to find SqlPlus processes running when executing shell script via Crontab

此生再无相见时 提交于 2019-12-08 12:12:49

问题


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)


来源:https://stackoverflow.com/questions/16192414/unable-to-find-sqlplus-processes-running-when-executing-shell-script-via-crontab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!