[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
nohup improve: output to another file
From: |
Pablo G. Figuerola |
Subject: |
nohup improve: output to another file |
Date: |
Fri, 13 Oct 2006 22:21:21 +0200 |
User-agent: |
Thunderbird 1.5.0.4 (X11/20060728) |
hi! I needed to use nohup but I don't want it to write output in nohup.out
I wrote 2 months ago a little hack to modify this. With it I do
"nohup -o my_output_file foo --options-to-foo" and I hope it tries to
write to my_output_file (but not to $HOME/my_output_file, unlike the
normal behavior with nohup.out). And I think it works well, at least
what I've tried.
I wrote it against coreutils-5.97. Maybe it is interesting for more
people, althought I don't know if it's well written, neither if it's any
other problem.
diff -u says this:
--- old/coreutils-5.97/src/nohup.c 2005-08-12 10:25:52.000000000 +0200
+++ new/coreutils-5.97/src/nohup.c 2006-07-31 00:22:45.000000000 +0200
@@ -44,6 +44,11 @@
NOHUP_FAILURE = 127
};
+/* Program options */
+struct option options[]={
+ {"output", required_argument, NULL, 'o'},
+ {NULL,0,NULL,0}};
+
char *program_name;
void
@@ -55,7 +60,7 @@
else
{
printf (_("\
-Usage: %s COMMAND [ARG]...\n\
+Usage: %s [-o <file>] COMMAND [ARG]...\n\
or: %s OPTION\n\
"),
program_name, program_name);
@@ -64,6 +69,7 @@
Run COMMAND, ignoring hangup signals.\n\
\n\
"), stdout);
+ fputs(_(" -o, --output <file>\t Redirects output to <file>
and not to 'nohup.out'\n"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
@@ -76,6 +82,7 @@
main (int argc, char **argv)
{
int saved_stderr_fd = STDERR_FILENO;
+ char *output_file=NULL;
initialize_main (&argc, &argv);
program_name = argv[0];
@@ -86,10 +93,20 @@
initialize_exit_failure (NOHUP_FAILURE);
atexit (close_stdout);
+ if(argc==1)
+ usage(NOHUP_FAILURE);
+
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);
- if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
- usage (NOHUP_FAILURE);
+
+ if(argv[1][0]=='-')
+ if('o'==getopt_long(argc, argv, "o:", options, NULL))
+ output_file=optarg;
+ else
+ usage(NOHUP_FAILURE);
+ else
+ if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
+ usage (NOHUP_FAILURE);
if (argc <= optind)
{
@@ -108,21 +125,26 @@
if (isatty (STDOUT_FILENO))
{
char *in_home = NULL;
- char const *file = "nohup.out";
+ char *file = "nohup.out";
int flags = O_CREAT | O_WRONLY | O_APPEND;
mode_t mode = S_IRUSR | S_IWUSR;
mode_t umask_value = umask (~mode);
int fd = fd_reopen (STDOUT_FILENO, file, flags, mode);
+ if(output_file)
+ file=output_file;
+
if (fd < 0)
{
int saved_errno = errno;
- char const *home = getenv ("HOME");
- if (home)
- {
- in_home = file_name_concat (home, file, NULL);
- fd = fd_reopen (STDOUT_FILENO, in_home, flags, mode);
- }
+ if(!output_file) {
+ char const *home = getenv ("HOME");
+ if (home)
+ {
+ in_home = file_name_concat (home, file, NULL);
+ fd = fd_reopen (STDOUT_FILENO, in_home, flags, mode);
+ }
+ }
if (fd < 0)
{
int saved_errno2 = errno;
@@ -132,7 +154,8 @@
quote (in_home));
exit (NOHUP_FAILURE);
}
- file = in_home;
+ if(!output_file)
+ file = in_home;
}
umask (umask_value);
No more.
Ciao
--
Pablo G. Figuerola
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- nohup improve: output to another file,
Pablo G. Figuerola <=