Tuesday, July 5, 2011

Daemonizing a linux process

Daemonizing can be done using the runuser command.

runuser - run a shell with substitute user and group IDs, similar to su, but will not run PAM hooks

Change the effective user id and group id to that of USER. No PAM hooks are run, and there will be no password prompt. This command is useful when run as the root user.
If run as a non-root user without privilege to set user ID, the command will fail.

-, -l, --login
make the shell a login shell

-c, --commmand=COMMAND
pass a single COMMAND to the shell with -c

-f, --fast
pass -f to the shell (for csh or tcsh)

-g, --group=GROUP
specify the primary group

-G, --supp-group=GROUP
specify a supplemental group

-m, --preserve-environment
do not reset environment variables

-p same as -m

-s, --shell=SHELL
run SHELL if /etc/shells allows it

--help display this help and exit

--version
output version information and exit

A mere - implies -l. If USER not given, assume root.

eXample :

$: id;runuser - rpksh -s /bin/bash -c "id; pwd";
uid=0(root) gid=0(root) groups=0(root)
uid=64206(rpksh) gid=100(users) groups=100(users)
/home/rpksh

in the above example i have execute the script as root, and the output is self explanatory.