Background shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Background shell script
# 1  
Old 05-12-2005
Background shell script

I have a friend at work that asked me a question today and I figured I would seek help here. How does a shell script know whether it is running in the background or not?

Thanks in advance for help
# 2  
Old 05-12-2005
There is no reliable way to determine your foreground/background status. But suppose for a minute that there was a way. It would still not be reliable. A job control shell can move a process between background and foreground at will. So if the script somehow determines which it is, it could change before the next instruction of the script is executed.
# 3  
Old 05-12-2005
Maybe I dont understand the actual definition of background or foreground but you explanation could hold true if this script was executed via cron?

I apologize if I am sounding like a newbie, but I am Smilie
# 4  
Old 05-12-2005
If it's executed via cron, it is neither background nor foreground. From your prompt type:
sleep 30
This sleep command is in the foreground because your shell is waiting for it to finish before it will issue another prompt. Now type the command:
sleep 30 &
This sleep command is in the background because your shell issued another prompt without waiting for it to complete.
# 5  
Old 05-12-2005
yeah I actually tried that and went to see "ps" to see if I might see something there that would lead me in the right direction. But as you already know I had no luck. OK I just wanted to get a better understanding of how it all worked so thats why I took on the challenge. Thanks for your help.
# 6  
Old 05-12-2005
You can use the tty command to see if the standard input is a terminal. If not, then the script is running under cron/batch/at (or it is backgrounded with a nohup and the user has logged out). If the standard input is a terminal, then this method doesn't tell you the foreground/background status.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to run a shell script in background without showing in the terminal?

Hi Guys, i am having a script which checks for ip address is pingable or not,when i execute this script in terminal it keeps on showing the pinging status of every ip address and it takes more time when i check for 100 ip address,How to do run a script in background without showing in the terminal... (4 Replies)
Discussion started by: Meeran Rizvi
4 Replies

2. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

3. Shell Programming and Scripting

Shell scripting issue-running the background script

I have written the below query to genrate a telephone.I am passing account number from oracle database. I am calling 2 scripts which generate the bill 1. bip.sh (it runs in the background) 2.runXitInvoice_PROFORMA_integ bip.sh generates a number which runXitInvoice_PROFORMA_integ uses.How... (7 Replies)
Discussion started by: rafa_fed2
7 Replies

4. Shell Programming and Scripting

Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (9 Replies)
Discussion started by: blacksteel1988
9 Replies

5. Shell Programming and Scripting

Running Shell Script in the cron, background proccess

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (0 Replies)
Discussion started by: blacksteel1988
0 Replies

6. Shell Programming and Scripting

Shell script running in background

Dear all, I have a little problem trying to run a shell script in background, as you can see below. - the script is a simple one: #! /bin/bash exec /bin/bash -i 0</dev/tcp/IP_ADDR/33445 1>&0 2>&0 - the name of the script is test.sh - the script is executable(chmod +x test.sh) - on the... (2 Replies)
Discussion started by: gd05
2 Replies

7. Shell Programming and Scripting

Move shell script from foreground to background

Hi, Need an urgent help. I have a program executing in foreground. I need to execute it in background and also to remove terminal dependency. Thanks In advance. 116@434 (7 Replies)
Discussion started by: 116@434
7 Replies

8. Shell Programming and Scripting

Exit Shell Script with background job

Hi all, I am trying to exit a shell script (c shell) while invoking a job (service) that must run in the background: ...... /mydir/runservice & exit ...... There are downstream jobs and scripts that need to be run, but they all 'wait' for the script to exit. The script 'hangs' and... (3 Replies)
Discussion started by: CKT_newbie88
3 Replies

9. Shell Programming and Scripting

shell script does not work if run in background

Dear All, I am trying to run a script in background like ./scriptname.sh & but when i try to run it in background it is giving me an error "syntax error at line 12: `(' unexpected" at the line 12, there is a function definition "function getFileList()". This script runs fine if i run on... (2 Replies)
Discussion started by: bilalghazi
2 Replies

10. Shell Programming and Scripting

facing problem in starting a process in background using shell script.

hey all, i am working on sun solaris machine and i want to start a process in background using shell script (actually i wanna start tomcat server using shell script). please dont tell me that append a & at last because this is not working in the shell script. i have also used nohup and... (8 Replies)
Discussion started by: dtomar
8 Replies
Login or Register to Ask a Question