execution in background


 
Thread Tools Search this Thread
Top Forums Programming execution in background
# 1  
Old 10-12-2004
execution in background

hey

im having trouble understanding how to execute commands in the background. i did some research and saw that you only have to fork and not wait for the child to finish. in another case you have to change the process group. i ve really tried both of them but i just cant get them to work. so i anyone feels like helping out, id appreciate it a lot. thanks

heres what i got so far.

a->background is set to 1 if one of the arguments on the command line equals '&'.

Code:
 /* create a process space */

      if ((pid = fork()) < 0) {

        perror ("Fork failed");
        exit(errno);
      }

      if (!pid) { // pid == 0

	printf(" \n if (!pid) = %d ", pid);

        printf (" child process[%d]\n", getpid ()); /* Display child PID */
        /* Child process */

	if (a->background == 1) {

// so when it comes across a '&', 

      	   if (fork () == 0) {

	     // just fork(); do not wait for the child
	     signal (SIGQUIT, originalQuitHandler); /* Oldhandler */
             setpgid (0, getpid ()); /* Change process group */
             execvp(a->com_name, a->argv);
	   }
	}

	else {

           /* This is the child, so execute the ls */ 
           execvp (a->com_name, a->argv);
	}
      }

      if (pid) {

	printf(" \n if (pid) = %d ", pid);
        
         /* We're in the parent; let's wait for the child to finish */
        waitpid (pid, NULL, 0);
      }
      	
	return;

thanks again

mile 1982 8O
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh in the background

Hi, I am trying to execute an ssh command in my script. ssh abcd@server_name After this command it actually logs in to the server asking for password prompt and then actually logs in to the server. I want all this to be happening in the background and show noithing in the output of my... (8 Replies)
Discussion started by: vital_parsley
8 Replies

2. Shell Programming and Scripting

Background Processes

Ok guys so I have my first dummy shell almost done except for one tiny part: I do not know how to run a process in the background, from the code! I already know how to do that in a normal shell: $ program & However, no clue when it comes to how to program that thing. :eek: A very... (2 Replies)
Discussion started by: Across
2 Replies

3. UNIX for Dummies Questions & Answers

Background Process

I need to submit a script that will continue to run after logging out and after a reboot or shutdown. I entered the following: nohup script & The script continues to run in the background after logging off the system but is killed after a reboot or shutdown. Any help would be greatly... (1 Reply)
Discussion started by: powwm
1 Replies

4. Shell Programming and Scripting

Expect Issue Serial Forground Execution vs Concurrent Background Execution

I have an expect script that interrogates several hundred unix servers for both access and directories therein using "ssh user@host ls -l /path". The combination of host/path are unique but the host may be interrogated multiple times if there are multiple paths to test. The expect script is run... (2 Replies)
Discussion started by: twk
2 Replies

5. Shell Programming and Scripting

Help in background process

Hi, I have a main script(main.ksh) within which I have called another script(sub.ksh). The sub.ksh script is made to run in the background using '&'. The main.ksh script logs the information in a logfile main_ddmmyy and the sub.ksh script also logs the information in the log file sub_ddmmyy.... (5 Replies)
Discussion started by: chella
5 Replies

6. UNIX for Dummies Questions & Answers

Parameters and background

Hello everyone, can you advice on how to execute a script passing parameters to it as well as requesting it to run in the background? It seems that my last character of the command line (& for background execution) gets taken in as an additional parameters and the job runs in the foreground.... (8 Replies)
Discussion started by: gio001
8 Replies

7. Shell Programming and Scripting

Background execution of a script

Hi All, Have a script called FTPIN-xx.sh. It does stuff and when all is done, it is to execute another. However, I'd like to have it execute the second script in the background or in a fashion that would allow my script to complete without having to wait. I'm robbing the command from how I... (2 Replies)
Discussion started by: Cameron
2 Replies

8. Shell Programming and Scripting

background jobs

Hi, i have a problem with turning a job into backgrund. When i enter this at the shell: spice -b darlington.cir -r output.raw > screenout.tmp & and then let me show the currently running jobs, i get the following output: + Suspended (tty output) spice -b darlington.cir -r output.raw >... (4 Replies)
Discussion started by: qsi
4 Replies

9. Shell Programming and Scripting

background process

Hi, In shell script when I use script1 >> filelog the echo statments of script1 gets printed in the filelog but when I try to run script in background i.e, script1 & >> filelog nothing gets printed in the filelog. Anybody knows whats going on here. thanks (3 Replies)
Discussion started by: k_oops9
3 Replies

10. Shell Programming and Scripting

process in background

Hi Guys, I am facing some weird problem with my shell script. The script shows up a menu and for every ontion internally calls a shell script which start/stop various servers. When I am using '&' while calling the internal shell script so that the server run in the background. For exiting... (1 Reply)
Discussion started by: agoyal
1 Replies
Login or Register to Ask a Question