How can I run Perl system commands in the background
2010-09-03 23:09
429 查看
这有一篇关于使用system函数在后台运行一个程序的文章,http://stackoverflow.com/questions/2711520/how-can-i-run-perl-system-commands-in-the-background
文中有如下几点需要注意:
1.Perl's system function has two modes:
(1)taking a single string and passing it to the command shell to allow special charactors to be processed.
(2)taking a list of strings,execing the first and passing the remaining strings as arguments
2.You can try using fork,.Both the main process and the background one(the 'child' process) share the same STDIN,STDOUT,and STDERR filehandles
.If both try to access them at once,strange things can happen.You may want to close or reopen these for the child.You can get around this with opening a pipe (see open in perlfunc) but on some system that the child process cannot outlive the parent.
3.Signals.
You'll have to catch the SIGCHLD signal,and possibly SIGPIPE too.SIGCHLD is sent when the backgrounded process finishs.SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die).This is not an issue with system("cmd").
4.Zombies
.You have to be prepared to "reap" the child process when it finishes.
$SIG{CHLD}=sub{wait}; or $SIG{CHLD}='IGNORE';
You can also use a double fork.You immdiately wait() for your first child,and the init daemon will wait() for your grandchild once it exits
unless($pid=fork){
unless(fork){
exec "what you really wanna do";
die "exec failed!";
}
exit(0);
}
waitpid($pid,0);
以上虽然是针对perl语言的,但这样情况同样也适应于C语言。
文中有如下几点需要注意:
1.Perl's system function has two modes:
(1)taking a single string and passing it to the command shell to allow special charactors to be processed.
(2)taking a list of strings,execing the first and passing the remaining strings as arguments
2.You can try using fork,.Both the main process and the background one(the 'child' process) share the same STDIN,STDOUT,and STDERR filehandles
.If both try to access them at once,strange things can happen.You may want to close or reopen these for the child.You can get around this with opening a pipe (see open in perlfunc) but on some system that the child process cannot outlive the parent.
3.Signals.
You'll have to catch the SIGCHLD signal,and possibly SIGPIPE too.SIGCHLD is sent when the backgrounded process finishs.SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die).This is not an issue with system("cmd").
4.Zombies
.You have to be prepared to "reap" the child process when it finishes.
$SIG{CHLD}=sub{wait}; or $SIG{CHLD}='IGNORE';
You can also use a double fork.You immdiately wait() for your first child,and the init daemon will wait() for your grandchild once it exits
unless($pid=fork){
unless(fork){
exec "what you really wanna do";
die "exec failed!";
}
exit(0);
}
waitpid($pid,0);
以上虽然是针对perl语言的,但这样情况同样也适应于C语言。
相关文章推荐
- How can I run two Django versions in the same server?
- How to Use `strace` to Trace the System Call in Linux
- [Yii Framework] How to run cron job (or use command) in the server with yii framework
- How can I get the IPv4 address of an interface in linux from C code ?
- 怎样控制Qt标题栏(How can I handle events in the titlebar and change its color etc ? )
- can I read/write the routing table in C without using system() command?
- How to fix “The system is running in low-graphics mode” Ubuntu 17.04 or 16.04
- How can I get an object's absolute position on the page in Javascript?
- How You Can Cash In On The Green Gold Rush of 2014
- How can i get hold of all of the visible items in my QListView?
- wireshark:no interface can be used for capturing in this system with the current configuration
- How one VBA project can use an object declared in the class module of another project
- wireshark在mac上使用时到遇到:no interface can be used for capturing in this system with the current configur
- How can I delete the row in TStringGrid component?
- 《How hard can it be? Estimating the difficulty of visual search in an image》和 PPT
- how can i print the data in DataGrid?
- How can convert character “%xx” in html using Perl
- How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕
- Fix the Can’t clobber writable file error in Perforce Version Control System - forward
- Android How to Check if the App is in Background