Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress system output when using nohup from Perl?

Tags:

perl

In Perl I am starting a process using the nohup command. The command is below:

system("nohup myproc pe88 &");

This works fine and the process starts as expected. However I would like to suppress the following output of this command - which is:

Sending output to nohup.out

I must have this process redirecting all of it's output to nohup.out but I just don't want it displayed when I run my Perl program. I want to instead, print my own user friendly message. I've tried a few variants but nothing has worked for me yet.

like image 277
sandeep Avatar asked Oct 03 '10 00:10

sandeep


People also ask

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.

How do I stop a process running with nohup?

You either need to save the PID when you do the nohup and use it later for the kill , or find the process by its command name in the ps -ef output and get the PID from that. You have to look for the command name, not nohup . @mbratch You should make that an answer instead of a comment.

What happens if you delete nohup out?

You can delete it if you don't want what's in it, but if the program that created the file is still running, then the disk space won't actually be reclaimed until the program exits.

What is nohup ignoring input?

nohup checks its standard input, standard output and standard error to see which are connected to a terminal; if it finds any which are connected, it handles them as appropriate (ignoring input, redirecting output to nohup. out , redirecting error to standard output), and tells you what it's done.


2 Answers

"Sending output to nohup.out" message is sent to STDERR, so you can catch the STDERR via the usual methods

  • either via shell: system("nohup myproc pe88 2> /tmp/error_log.txt &");

    Use /dev/null instead of /tmp/error_log.txt if you don't need stderr at all; and add "> /tmp/myout.txt" to redirect stdout.

  • Or by capturing via Perl (don't use system() call, instead use IPC::Open3 or capture command from IPC::System::Simple)

like image 116
DVK Avatar answered Nov 15 '22 11:11

DVK


How about:

system("nohup myproc pe88 >nohup.out 2>&1 &");

The man page for nohup says:

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'.

So if you explicitly redirect STDOUT and STDERR to nohup.out, then nohup doesn't print that message. Granted, you don't get the automatic fallback to $HOME/nohup.out if nohup.out is unwritable, but you can check for that first if that's an issue.

Note that if you redirect just STDOUT, nohup prints a "redirecting stderr to stdout" message.

like image 23
Michael Krebs Avatar answered Nov 15 '22 12:11

Michael Krebs