linux程序后台调度代码写法(没有终端)
2014-04-26 23:34
197 查看
/* 该段代码要在进程的开写,不然close那里可能出异常 */
int initprocess( const char * workdir)
{
int i;
pid_t pid;
if ( (pid = fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* parent terminates */
/* child 1 continues... */
if (setsid() < 0) /* become session leader */
return (-1);
if( signal( SIGHUP, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGPIPE, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGALRM, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGCHLD, SIG_IGN ) != SIG_ERR )
{
}
if ( (pid = fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* child 1 terminates */
/* child 2 continues... */
if( NULL != workdir ) chdir( workdir ); /* change working directory */
/* close off file descriptors */
for (i = 0; i < 64; i++)
close(i);
/* redirect stdin, stdout, and stderr to /dev/null */
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
return (0); /* success */
}
int initprocess( const char * workdir)
{
int i;
pid_t pid;
if ( (pid = fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* parent terminates */
/* child 1 continues... */
if (setsid() < 0) /* become session leader */
return (-1);
if( signal( SIGHUP, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGPIPE, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGALRM, SIG_IGN ) != SIG_ERR )
{
}
if( signal( SIGCHLD, SIG_IGN ) != SIG_ERR )
{
}
if ( (pid = fork()) < 0)
return (-1);
else if (pid)
_exit(0); /* child 1 terminates */
/* child 2 continues... */
if( NULL != workdir ) chdir( workdir ); /* change working directory */
/* close off file descriptors */
for (i = 0; i < 64; i++)
close(i);
/* redirect stdin, stdout, and stderr to /dev/null */
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
return (0); /* success */
}
相关文章推荐
- linux程序后台调度代码写法(没有终端)
- linux下程序的前、后台切换及让进程不受终端的影响
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 【整理】解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】
- Linux前台的程序转到后台执行(关闭终端而不杀死命令)
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】
- 4.Linux下nohup命令实现退出终端后程序继续后台运行
- 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 让Linux终端中执行的程序在后台运行
- Linux前台的程序转到后台执行(关闭终端而不杀死命令)
- Linux前台的程序转到后台执行(关闭终端而不杀死命令)
- 解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】
- 【转】Linux下nohup命令实现退出终端后程序继续后台运行
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- Linux终端下后台运行程序被Stopped的原因以及解决