can a nohup'ed ksh script interact with user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting can a nohup'ed ksh script interact with user
# 8  
Old 05-29-2012
You don't have to use nohup, by-the-by. It only does things that your script can do by itself. So you could do it all on one script still perhaps:

Code:
#!/bin/ksh

some_foreground_stuff

echo "Input some data"
read SOMEVAR

(
        some_background stuff
        more background stuff
        etc
) >>nohup.out 2>&1 </dev/null & disown

echo "Background stuff is running"
exit 0

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 05-30-2012
Very kewl, did not know about "disown". Regrettably the version of ksh on this server (Solaris 10) does not support disown. Why did you use "</dev/null", I know what it is but not why you used it.

Anyway while disown is not an option for the version of ksh being used , this did lead me to find the "-p" option for nohup. A test script similar to your example shows it appears to serve the same purpose:
Code:
#!/usr/bin/env ksh
 
interact with user

echo "script  will now run independently in background"
 
nohup -p $$
 
( 
  so something
  ...
)&
 
exit 0

# 10  
Old 05-30-2012
Quote:
Originally Posted by twk
Very kewl, did not know about "disown". Regrettably the version of ksh on this server (Solaris 10) does not support disown.
If it doesn't support it, you don't even need it, so just remove it.

Quote:
Why did you use "</dev/null", I know what it is but not why you used it.
Because that's exactly what nohup does. Things that try to read from /dev/null will instantly get 'end of file', which is better than an error like 'invalid file descriptor' that you'd get from actually closing the file. See man nohup.

Quote:
Code:
#!/usr/bin/env ksh
 
interact with user

echo "script  will now run independently in background"
 
nohup -p $$
 
( 
  so something
  ...
)&
 
exit 0

I don't know what -p does, my system doesn't have it. But the nohup is entirely redundant here. Your script also doesn't work like it should since you're not doing redirections on the part inside the subshell anymore. If you just did redirections the way I did in the first place instead of ripping them out...

Code:
#!/usr/bin/env ksh
 
interact with user

echo "script  will now run independently in background"
 
( 
  so something
  ...
) >>nohup.log 2>&1 </dev/null &
 
exit 0

# 11  
Old 05-30-2012
If the nohup is redundant in my example would not the disown be redundent as well? or to put it another way what does the disown do that the redirection in conjunction with putting the script in the backgound not do?

As to the redirection of script output, the "real" script already redirects its output to a file defined at run time.

From the man nohup page:

/usr/bin/nohup -p [-Fa] pid [pid]...
-p Operates on a list of processes.
-F Force. Grabs the target processes even if another pro-
cess has control.
-a Always changes the signal disposition of target
processes.
# 12  
Old 05-30-2012
Quote:
Originally Posted by twk
If the nohup is redundant in my example would not the disown be redundent as well?
Some shells won't actually quit after backgrounding something, they'll wait for background jobs. To tell it to give up and leave processes behind, you must disown them first.

Shells which don't have "disown", don't do that anyway, so you don't need it.

Quote:
or to put it another way what does the disown do that the redirection in conjunction with putting the script in the backgound not do?
Some shells need it to leave a subshell or background process running around by itself. Some don't. Yours doesn't.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh behavior in scripts spawned w/nohup

I have a need to run any number of identical scripts simultaneously, so I've created a driver script which reads a template script, edits these appropriately and then submits them via nohup. The spawned scripts all check to see at some point how many of their number are running and once the count... (7 Replies)
Discussion started by: safedba
7 Replies

2. Shell Programming and Scripting

I need to save a pid of a child started with $: su <user> -c “nohup …”

Hello, I want to save pid of a child process but I get empty file. su myuser -c "nohup ./mydaemon.sh >/dev/null 2>&1 & print $! > mydaemon.pid" This one works: nohup ./mydaemon.sh >/dev/null 2>&1 & print $! > mydaemon.pid Please help. Thank you in advance. (2 Replies)
Discussion started by: vincegata
2 Replies

3. Shell Programming and Scripting

Expect script - Interact help

Hi Guys, Further to my post yesterday I have got round the issue of not being able to use expect by using one of our unix machines to have the script running instead of the jumpbox itself. However my issue is I now have an extra bit it the script which is shh to the jumpbox which requires a ras... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. UNIX for Advanced & Expert Users

Help with ksh script to list, then cp files from a user input date range

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (1 Reply)
Discussion started by: chococrunch6
1 Replies

5. Programming

How could I interact with shell script from webportal written in php?

Hello, I am new on PHP scripting .I have shell scripts which I an running currently from linux server but now I want to make a web portal from where I will run all my scripts but the problem is all my scripts ask for parameters so I am getting confused how could I run my shell script from web... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

6. Shell Programming and Scripting

Expect script exiting too fast if used without interact.

Hi I'm working on an Expect script that is supposed to log-into a remote server and run some steps and exit. In the script I first spawn a 'ssh' session to the server and then after logging in I 'send' all the necessary steps ( with a '\r' at the end, so that they get automatically executed the... (3 Replies)
Discussion started by: clakkad
3 Replies

7. UNIX for Dummies Questions & Answers

Expect "interact" fails when called from another script

So, I have an expect script (let's call it expect.exp) that takes 3 arguments. It logs into a remote server, runs a set of commands, then hands control over to the user by the "interact" command. If I call this script from the command line, it works properly. Now I'd like to apply this script... (2 Replies)
Discussion started by: treesloth
2 Replies

8. Shell Programming and Scripting

How to make shell script interact with program

Hello all! I have a C program that runs on a loop, prompting the user for input until it is exited. I want to create a shell script that can run this program and provide input. How can I do this? I have investigated 'expect' and piping to stdin, but haven't had any success. Any help is... (2 Replies)
Discussion started by: radish04
2 Replies

9. UNIX for Advanced & Expert Users

Find the process was run using nohup or ksh.

Hi, I want to write a script which should be run only on foreground. Is there any way that the script can check itself whether it was run using nohup or ksh and if the user runs the script using nohup then it should prompt the user to run it using ksh? If (The user triggers the script using... (4 Replies)
Discussion started by: RRVARMA
4 Replies

10. Shell Programming and Scripting

can a shell script interact with database?

Hi All, Language like C,Java can interact with database..and can use database information .. can a shall script do this? if yes thn plz guide me..... thankx (7 Replies)
Discussion started by: johnray31
7 Replies
Login or Register to Ask a Question