LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2013, 06:40 PM   #1
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
commands after nohup jobs finish


Hello everyone,

I am running multiple nohup jobs from a shell script and want to run other commands after all of these nohup jobs are finished. How can I do that? Thanks!

Code:
num_lines=$(wc -l < "$FILE");

for ((counter=1;counter<="$num_lines";counter++));do

	col_name=$(sed -n "$counter"p "$FILE" | awk -F":" '{print $2}');
	echo "#!/bin/bash" >> "$col_name".sh;
        #contents is a file in the same directory that contains the logic
	cat contents >> "$col_name".sh;
	chmod +x "$col_name".sh;
	nohup "$col_name".sh > log_"$col_name" &

done

#Now I want to wait till all the nohup jobs are completed and then use some commands on the output files generated by each "$col_name".sh.
 
Old 03-25-2013, 06:58 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Never tried it with nohup, but you can wait on background processes with the "wait" command, eg:
Code:
#!/bin/bash

./code args &

# do stuff while "code" is running

wait %1

# do stuff after "code" has finished
 
Old 03-26-2013, 05:31 PM   #3
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Original Poster
Rep: Reputation: 2
wait %1 does not work with nohup. I submitted couple of jobs using nohup but before all of them were finished main script started processing next commands even though I used wait %1. any other alternatives?
 
Old 03-26-2013, 07:23 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Just run wait without %1:
Code:
nohup cmd &

# wait for every child process to finish
wait
 
1 members found this post helpful.
Old 03-28-2013, 08:36 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
What about storing stuff in an array with the PID?

Code:
nohup command &
NOHUPS[${#NOHUPS]=$!
nohup command2 &
NOHUPS[${#NOHUPS]=$!
Then send kill -0 to all those PIDs to see if they're still alive (or not) and continue when all of them have finished...
 
Old 03-28-2013, 09:44 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by ip_address View Post
wait %1 does not work with nohup. I submitted couple of jobs using nohup but before all of them were finished main script started processing next commands even though I used wait %1. any other alternatives?
If you're going to wait for the jobs to finish before continuing on with the script anyway, then why do you need nohup?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nohup and Background Jobs Onyx2316 Linux - Newbie 3 02-28-2012 12:10 AM
Difference between nohup and bg commands? tuhin_chatt Linux - General 2 05-08-2010 08:39 AM
How to test disown and nohup commands ? gjagadish Programming 2 03-21-2010 04:33 AM
Jobs started with nohup aborts after closing ssh awalls Linux - Software 4 09-28-2007 05:26 AM
nohup not preventing jobs from getting killed TrulyTessa Linux - Newbie 4 12-24-2004 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration