Nohup Create New Files Nohup.Out by Day

nohup create new files nohup.out by day

I think one solution for this is using Supervisord.

Below a example of /etc/supervisord.conf

...
[program:coherence]
command=java -XX:MaxHeapFreeRatio=70 -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprof -jar $iscsiJar &
directory=/u01/jdk
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/java.err.log
stdout_logfile=/var/log/java.out.log
user=oracle
stopsignal=KILL
killasgroup=true
stopasgroup=true

Using supervisord you can stop/start this process like was a service.

How do I use the nohup command without getting nohup.out?

The nohup command only writes to nohup.out if the output would otherwise go to the terminal. If you have redirected the output of the command somewhere else - including /dev/null - that's where it goes instead.

 nohup command >/dev/null 2>&1   # doesn't create nohup.out

Note that the >/dev/null 2>&1 sequence can be abbreviated to just >&/dev/null in most (but not all) shells.

If you're using nohup, that probably means you want to run the command in the background by putting another & on the end of the whole thing:

 nohup command >/dev/null 2>&1 & # runs in background, still doesn't create nohup.out

On Linux, running a job with nohup automatically closes its input as well. On other systems, notably BSD and macOS, that is not the case, so when running in the background, you might want to close input manually. While closing input has no effect on the creation or not of nohup.out, it avoids another problem: if a background process tries to read anything from standard input, it will pause, waiting for you to bring it back to the foreground and type something. So the extra-safe version looks like this:

nohup command </dev/null >/dev/null 2>&1 & # completely detached from terminal 

Note, however, that this does not prevent the command from accessing the terminal directly, nor does it remove it from your shell's process group. If you want to do the latter, and you are running bash, ksh, or zsh, you can do so by running disown with no argument as the next command. That will mean the background process is no longer associated with a shell "job" and will not have any signals forwarded to it from the shell. (A disowned process gets no signals forwarded to it automatically by its parent shell - but without nohup, it will still receive a HUP signal sent via other means, such as a manual kill command. A nohup'ed process ignores any and all HUP signals, no matter how they are sent.)

Explanation:

In Unixy systems, every source of input or target of output has a number associated with it called a "file descriptor", or "fd" for short. Every running program ("process") has its own set of these, and when a new process starts up it has three of them already open: "standard input", which is fd 0, is open for the process to read from, while "standard output" (fd 1) and "standard error" (fd 2) are open for it to write to. If you just run a command in a terminal window, then by default, anything you type goes to its standard input, while both its standard output and standard error get sent to that window.

But you can ask the shell to change where any or all of those file descriptors point before launching the command; that's what the redirection (<, <<, >, >>) and pipe (|) operators do.

The pipe is the simplest of these... command1 | command2 arranges for the standard output of command1 to feed directly into the standard input of command2. This is a very handy arrangement that has led to a particular design pattern in UNIX tools (and explains the existence of standard error, which allows a program to send messages to the user even though its output is going into the next program in the pipeline). But you can only pipe standard output to standard input; you can't send any other file descriptors to a pipe without some juggling.

The redirection operators are friendlier in that they let you specify which file descriptor to redirect. So 0<infile reads standard input from the file named infile, while 2>>logfile appends standard error to the end of the file named logfile. If you don't specify a number, then input redirection defaults to fd 0 (< is the same as 0<), while output redirection defaults to fd 1 (> is the same as 1>).

Also, you can combine file descriptors together: 2>&1 means "send standard error wherever standard output is going". That means that you get a single stream of output that includes both standard out and standard error intermixed with no way to separate them anymore, but it also means that you can include standard error in a pipe.

So the sequence >/dev/null 2>&1 means "send standard output to /dev/null" (which is a special device that just throws away whatever you write to it) "and then send standard error to wherever standard output is going" (which we just made sure was /dev/null). Basically, "throw away whatever this command writes to either file descriptor".

When nohup detects that neither its standard error nor output is attached to a terminal, it doesn't bother to create nohup.out, but assumes that the output is already redirected where the user wants it to go.

The /dev/null device works for input, too; if you run a command with </dev/null, then any attempt by that command to read from standard input will instantly encounter end-of-file. Note that the merge syntax won't have the same effect here; it only works to point a file descriptor to another one that's open in the same direction (input or output). The shell will let you do >/dev/null <&1, but that winds up creating a process with an input file descriptor open on an output stream, so instead of just hitting end-of-file, any read attempt will trigger a fatal "invalid file descriptor" error.

where will be nohup.out file created when shell script is executed by Java Runtime?

nohup only writes the output to a file if it's connected to a terminal. This won't be the case when you invoke it through exec, so the output will be written to the sub-process's stdout (which you can get through getInputStream). No nohup.out will be created anywhere.

How to manage nohup.out file in Tornado?

@jujaro 's answer is quite helpful and I tried logging module in my web service. However, there are still some restrictions to use logging in Tornado. See the other question asked.

As a result, I tried crontab in linux to create a cron job at midnight (use crontab -e in linux shell):

59 23 * * * source /home/zfz/cleanlog.sh

This cron job launches my script cleanlog.sh at 23:59 everyday.

The contents of clean.sh:

fn=$(date +%F_service_log.out)
cat /home/zfz/nohup.out >> "/home/zfz/log/$fn"
echo '' > /home/zfz/nohup.out

This script creates a log file with the date of the day ,and echo '' clears the nohup.out in case it grows to large. Here are my log files split from nohup.out by now:

-rw-r--r-- 1 zfz zfz  54474342 May 22 23:59 2013-05-22_service_log.out
-rw-r--r-- 1 zfz zfz 23481121 May 23 23:59 2013-05-23_service_log.out

Can I change the name of `nohup.out`?

nohup some_command &> nohup2.out &

and voila.


Older syntax for Bash version < 4:

nohup some_command > nohup2.out 2>&1 &

How can I redirect nohup output to a specified file?

nohup dotnet application.dll > out.log 2>&1 & is the correct form.

> out.log redirects STDOUT to the file out.log.

2>&1 redirects fd2 (STDERR) to fd1 (STDOUT), which is already redirected to the file out.log.

Your problem lies in the file permissions (or a read-only filesystem). Prepending sudo to your command can't fix this, because only nohup dotnet application.dll is executed as root by sudo, the output redirection is done by bash with your normal user privileges. You can work around this by calling a separate shell with root privileges:

sudo sh -c 'nohup dotnet application.dll > out.log 2>&1 &'

Where will be nohup file created/stored

Could you check home directory.

also you can redirect as below;

nohup /usr/hp/ism/jboss-3.2.8.SP1/bin/run.sh &> /tmp/nohup.out

man nohup ;

If standard input is a terminal, redirect it from /dev/null. If
standard output is a terminal, append output to 'nohup.out' if
possible, '$HOME/nohup.out' otherwise. If standard error is a
terminal, redirect it to standard
output. To save output to FILE, use 'nohup COMMAND > FILE'.

can i delete nohup.out, Because it stopped writing?

I assume you started your program using nohup like this:

nohup your_program maybe some arguments &

The nohup command will redirect the output to nohup.out before starting your program. Your program does not know that the output is redirected. That's why you cannot make it recreate the output file nohup.out.

If you remove nohup.out you will only remove the directory entry. The file will still exist as an inode on your disk with all the data until your program terminates (or closes stdout and stderr), but you can no longer access the file by its name.

If you don't see any new output, then your program doesn't produce any. Note that the output is probably buffered, so it will appear in the file only when the program has written a certain amount of data.
You have to check what your program is doing. It might be blocked somehow or you might have a bug in your program.

Maybe you can use strace to check what system calls are done by your program.



Related Topics



Leave a reply



Submit