Storing process Id of a nohup command

Hi,

I am running below code:

 
for i in `ls`
do
nohup sqlldr userid=apps/apps data=data01.dat log=my1.log control=my.ctl bad=my1.bad direct=yes silent=all parallel=true &
done
 

This will run the sqlldr command in parallel as a background process.
I want to store the process Id each time the command is run and store it in a Oracle table with status if the process is running or completed.

Any kind of help is highly appreciated.
Thanks.

you can gather the process id using $! , store it into a file and at the end, load them into the table

for i in `ls`
do
nohup sqlldr userid=apps/apps data=data01.dat log=my1.log control=my.ctl bad=my1.bad direct=yes silent=all parallel=true &
Process_ID=$!
echo "${Process_ID}" >> file.out
done

anyways, it if is oracle, v$session table will hold the current running sql processes id

SELECT process FROM v$session WHERE username = 'SCOTT';
1 Like

Thanks for help,
Could you also tell me After storing the process Id how can i check if that particular process is running or completed?

if you want to wait till that process is completed,

wait ${Process_ID}

You can capture multiple process id's is different variables and run the above command to wait for each

---------- Post updated at 05:00 AM ---------- Previous update was at 04:55 AM ----------

if [[ $(ps -eaf | awk -v pid="${Process_ID}" '$2 == pid {} END {print NR}') > 0 ]]; then
echo "Running";
else
echo "Completed";
fi
1 Like

How about this ?

#!/bin/bash

sqlplus -s apps/apps << EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
delete from tablename;
exit;
EOF


for i in `ls`
do
nohup sqlldr userid=apps/apps data=data01.dat log=my1.log control=my.ctl bad=my1.bad direct=yes silent=all parallel=true &
Process_ID=$!

sqlplus -s apps/apps << EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
insert into tablename (id,status) values ($Process_ID,'Running');
exit;
EOF

done

RunningProcess {

count=$(sqlplus -s apps/apps << EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
select count(id) from tablename where status='Running';
exit;
EOF)

}

RunningProcess;
while [ $count -gt 0 ]
do

echo "
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
select id from tablename where status='Running';
" |sqlplus -s apps/apps | while read Pid
do
      ps -f $Pid 2>/dev/null
      if [ $? -eq 1 ]
      then
          sqlplus -s apps/apps << EOF
          whenever sqlerror exit sql.sqlcode;
          set echo off
          set heading off
          update tablename set status='Completed' where id=$Pid;
          exit;
          EOF
      fi
  done
  RunningProcess;
done
1 Like

Thanks for detailed help!

---------- Post updated at 05:08 AM ---------- Previous update was at 05:02 AM ----------

when I run the command
ps -f 29097
echo $? prints 1 although this process has already completed.
Any idea why its happening

---------- Post updated at 05:24 AM ---------- Previous update was at 05:08 AM ----------

when I do ps -f 29097
it displays: PID TTY STAT TIME COMMAND
and the status of command is false...
i.e
echo $?
1

Hi,

Updated my previous post. Please check "if" condition

Thnx,
Pravin

if $? is '1' after ps -f <pid> command, and if your pid is correct the the process is complete