lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Just to add, a well behaved daemon should change its working dir to
"/" and redirect stdin/out/err to /dev/null.

Also if you have an open file descriptor at this point (does Lua
have?), then you should check if its <= 2 before redirecting
stdin/out/err, since when true you were started by a consoleless
daemon already and you might close your fds.

Either do it in C or use luaposix or alien.

On Mon, Jan 3, 2011 at 2:42 PM, Steve Litt <slitt@troubleshooters.com> wrote:
> Hi all,
>
> My UMENU program (perl version) can run a program in the background like this:
>
> ./ufork.pl command_to_be_run_in_background
>
> Here's the code of ufork.pl:
> =======================================
> #!/usr/bin/perl -w
> use strict;
>
> use POSIX qw(setsid);
>
> sub launch(@) {
>    my(@args) = @_;
>    unless (fork) {
>        setsid;             # set child process to new session
>        unless (fork) {     # Create a grandchild
>            close STDIN;    # close std files
>            close STDOUT;
>            close STDERR;
>            exec @args;     # exec the x program
>        }
>    }
>    sleep(1);
> }
>
> launch(@ARGV);
> =======================================
>
> In my Lua readings I haven't come across a fork() command. Is there a way I
> can rewrite my ufork.pl in Lua? If not, is there a different way to do it in
> Lua? If not that, I spoze I could do it in C and make sure it's statically
> compiled so as to be distributable as a binary.
>
> Anyway, anyone know how to do this in Lua?
>
> Thanks
>
> SteveT
>
> --
> Steve Litt
> Recession Relief Package
> http://www.recession-relief.US
> Twitter: http://www.twitter.com/stevelitt
>
>
>