Sponsored Content
Full Discussion: Background Processes
Top Forums Shell Programming and Scripting Background Processes Post 302401427 by abubacker on Saturday 6th of March 2010 04:17:11 AM
Old 03-06-2010
MySQL Solution

If you want to run the process background programmatically , then you must make the process as a daemon.

I :

* The first step is that change the current directory as root "/" because it provide the
facility to do all the operation from the root
* If you want to mount a file system then you are unable to do this with out changing cur_dir into a root dir * what are all the path name you planned to give that should be in a absolute path name

II:

* Create a process and kill the parent of that process
* It will make the living process ( child ) as a child of init() and its process id will become 1
* Though a we never need a parent for a daemon

III :
* Daemon should be a session leader
* This can be achieved using setsid() in C

IV :
* We don't need an interaction of user for daemon process
* So we close all the descriptor
* We should assure that no other file descriptor would be assign for this process

V :
* Output and the monitoring the daemon will be done using a log files
* It is better to log the information in a /var/log/ directory structure
VI :
* Daemon should always in a running state
* So we should secure the daemon from the signals
* We should ignore all the signals except SIGHUP
* As a convention SIGHUP uses for reading configuration file
VII :
* Have a termination handler
* We should do some operation whenever we terminate the daemon
* Like releasing the memory closing the configuration file etc .




Here I provide an example code :
Code:
/* header files */
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <sys/wait.h>

/* Global variables */
...


/* Function prototypes: */
...
void terminate (int signum); /* clean up before termination */


int
main (void)
{
  ...

  if (chdir (ROOT_DIR))         /* change to directory containing data files */
   {
     fprintf (stderr, "`%s': ", ROOT_DIR);
     perror (NULL);
     exit (1);
   }

   /* Daemonize a process  */



   switch (fork ())
     {
     case -1:                    /* can't fork */
       perror ("fork()");
       exit (3);
     case 0:                     /* child, process becomes a daemon: */
       close (STDIN_FILENO);
       close (STDOUT_FILENO);
       close (STDERR_FILENO);
       if (setsid () == -1)      /* request a new session (job control) */
         {
           exit (4);
         }
       break;
     default:                    /* parent returns to calling process: */
       return 0;
     }

   /* Establish signal handler to clean up before termination: */



   if (signal (SIGTERM, terminate) == SIG_IGN)
     signal (SIGTERM, SIG_IGN);
   signal (SIGINT, SIG_IGN);
   signal (SIGHUP, SIG_IGN);

   /* Main program loop */
   /* Operation done here */

   while (keep_going)
     {
       ...
     }

   return 0;
}



/* Termination handler */

void  terminate (int signum)
{
  keep_going = 0;
  signal (signum, termination_handler);
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Background processes

How do you capture the return code from a background process? I am dumping data to a fifo and then processing it in a c program. I need to know that the sql finished successfully to ensure no missing data. Thanks. ex. sqlplus user/password < get_data.sql > data_fifo.txt & bin/process_data... (2 Replies)
Discussion started by: korndog
2 Replies

2. Shell Programming and Scripting

Running two processes in background

hi there, here's what i need in my korn-shell: ... begin korn-shell script ... nohup process_A.ksh ; nohup process_B.ksh & ... "other stuff" ... end lorn-shell script in plain english i want process A and process B to run in the background so that the script can continue doing... (6 Replies)
Discussion started by: jacob_gs
6 Replies

3. Programming

Background processes in a dummy shell...

Hey guys, I am writing a very simple dummy shell in C++ and I am having trouble getting a process to run in the background. First of all, the shell has to recognize when I input a "&" at the end of the command, then it has to stick it in the background of the shell. I understand that if I want... (6 Replies)
Discussion started by: icer
6 Replies

4. Linux

Question about background processes

Hi! First of all, let me warn you I'm quite new to the world of LINUX and Operating Systems understanding, so that's why I pose these newbie and stupid qustions... Anyway, I'm trying to build my own simple shell in C and I'm getting some problems in implementing the background process ('&')... (10 Replies)
Discussion started by: neimaD
10 Replies

5. SuSE

oracle background processes

I have installed oracle 10g on suse sles9. I do not see oracle background processes. ps -ef|grep ora_ gives me environment variables junk. ps -ef|grep smon does not show anything however database is up and running. Any idea how to tweak that? (1 Reply)
Discussion started by: vijayasawant
1 Replies

6. Shell Programming and Scripting

Keep a certain number of background processes running

I've got a bit of code I'm trying to work on... What i want to happen is ... at all times have four parallel mysql dump and imports running. I found the follow code snippet on the forum and modified it to work by starting four concurrent processes but it waits until all four are done before... (7 Replies)
Discussion started by: dgob123
7 Replies

7. UNIX for Dummies Questions & Answers

Disadvantage of background processes

Hi, Inorder to improve the performance, I am trying to execute my command as a background process.. For eg: To zip large numbers of files present in a directory instead of using a single process, i do follow the below method: gunzip -c > / &... (3 Replies)
Discussion started by: unni.raj
3 Replies

8. Solaris

About running processes in background

Hi, I need to establish a procedure that will start an application in background each time my remote Solaris server is (re)started. This would be a kind of daemon. I am no sysadmin expert, so I am looking for pointers. How should I proceed? What are the main steps? Thanks, JVerstry (9 Replies)
Discussion started by: JVerstry
9 Replies

9. Shell Programming and Scripting

Need help on background processes

Hi, I have a schell script parent.ksh from which I am calling three background processes a.ksh,b.ksh and c.ksh. Once these three processes completes the next step in parent.ksh should execute. How to achieve this? Please help me.... Thanks... (1 Reply)
Discussion started by: ravinunna
1 Replies

10. UNIX for Advanced & Expert Users

List all background processes

How do I list the process in a Unix based system which are running in background? The following are options that I'm aware of, but they may not be appropiate. a. using ps -ef , and getting records of processes for which STATUS='S'(uninterruptible sleep) b. using jobs -l, and filtering... (5 Replies)
Discussion started by: kumarjt
5 Replies
SHELL-QUOTE(1)						User Contributed Perl Documentation					    SHELL-QUOTE(1)

NAME
shell-quote - quote arguments for safe use, unmodified in a shell command SYNOPSIS
shell-quote [switch]... arg... DESCRIPTION
shell-quote lets you pass arbitrary strings through the shell so that they won't be changed by the shell. This lets you process commands or files with embedded white space or shell globbing characters safely. Here are a few examples. EXAMPLES
ssh preserving args When running a remote command with ssh, ssh doesn't preserve the separate arguments it receives. It just joins them with spaces and passes them to "$SHELL -c". This doesn't work as intended: ssh host touch 'hi there' # fails It creates 2 files, hi and there. Instead, do this: cmd=`shell-quote touch 'hi there'` ssh host "$cmd" This gives you just 1 file, hi there. process find output It's not ordinarily possible to process an arbitrary list of files output by find with a shell script. Anything you put in $IFS to split up the output could legitimately be in a file's name. Here's how you can do it using shell-quote: eval set -- `find -type f -print0 | xargs -0 shell-quote --` debug shell scripts shell-quote is better than echo for debugging shell scripts. debug() { [ -z "$debug" ] || shell-quote "debug:" "$@" } With echo you can't tell the difference between "debug 'foo bar'" and "debug foo bar", but with shell-quote you can. save a command for later shell-quote can be used to build up a shell command to run later. Say you want the user to be able to give you switches for a command you're going to run. If you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are things the user can't pass through), you can do something like this: user_switches= while [ $# != 0 ] do case x$1 in x--pass-through) [ $# -gt 1 ] || die "need an argument for $1" user_switches="$user_switches "`shell-quote -- "$2"` shift;; # process other switches esac shift done # later eval "shell-quote some-command $user_switches my args" OPTIONS
--debug Turn debugging on. --help Show the usage message and die. --version Show the version number and exit. AVAILABILITY
The code is licensed under the GNU GPL. Check http://www.argon.org/~roderick/ or CPAN for updated versions. AUTHOR
Roderick Schertler <roderick@argon.org> perl v5.16.3 2010-06-11 SHELL-QUOTE(1)
All times are GMT -4. The time now is 08:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy