nohup.c 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* nohup.c - run commandline with SIGHUP blocked.
  2. *
  3. * Copyright 2011 Rob Landley <rob@landley.net>
  4. *
  5. * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
  6. USE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(125)))
  7. config NOHUP
  8. bool "nohup"
  9. default y
  10. help
  11. usage: nohup COMMAND...
  12. Run a command that survives the end of its terminal.
  13. Redirect tty on stdin to /dev/null, tty on stdout to "nohup.out".
  14. */
  15. #include "toys.h"
  16. void nohup_main(void)
  17. {
  18. toys.exitval = 125;
  19. xsignal(SIGHUP, SIG_IGN);
  20. if (isatty(1)) {
  21. close(1);
  22. if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
  23. S_IRUSR|S_IWUSR ))
  24. {
  25. char *temp = getenv("HOME");
  26. temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
  27. xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
  28. free(temp);
  29. }
  30. }
  31. if (isatty(0)) {
  32. close(0);
  33. xopen_stdio("/dev/null", O_RDONLY);
  34. }
  35. toys.exitval = 0;
  36. xexec(toys.optargs);
  37. }