nohup command could not generate the nohup.out file by rundeck job - nohup

My rundeck job execute a simple nohup command "cd /tmp/; nohup ls -l &". the job execution log will get the command output but not generate the nohup.out file in that directory.
i used all kind of steps in rundeck workflow. all couldnot generate the nohup.out file.
Do anyone have any idea why rundeck couldnot generate the nohup.out file but send the command output to the execution logs?? thank

You didn't seem to redirect the output to a file, nohup works fine in RunDeck from my experience.
cd /tmp/; nohup ls -l > /tmp/nohup.something

I have a working theory: nohup is being consumed/interpreted by the Rundeck infrastructure and interpreted approximately as: 'run this job indefinitely in the background' without our usual understanding of 'with output to a non-tty' (i.e. both stdout/stderr to nohup.out or piped together to file). Or at least, I am fairly certain it is not executing the version of nohup that is on my path.
Thus:
you should manually redirect all of the output (stdout and stderr) to file unless you are content to let Rundeck capture it
you do not need the &, although it does not hurt anything
In my case, I translated from:
cd tools/celery && nohup ./start.sh flower > log_name.txt &
to:
cd tools/celery && nohup ./start.sh flower > log_name.txt 2>&1

Related

Logs are not inserting in 'nohup.out' file

I am executing script1.sh through crontab.
script1.sh
echo "Crontab is executing this script."
echo "Some code is running here."
cd cron
./script2.sh
'script1.sh' is invoking 'script2.sh'.
'script2.sh'
echo "Exceuting script2."
echo "log of script3.sh is inserting in nohup.out file."
nohup sh script3.sh &
echo "Above syntax is not logging in nohup.out file. but this syntax is 'sh script3.sh > nohup.out &' is logging in nohup.out file. Why is it so?"
'script2.sh' is invoking 'script3.sh',but not able to log in nohup.out file. For logging following syntax is used.
nohup sh script3.sh &
'script3.sh' contains below mention line of codes.
echo "Executing script3. This is just test example to simulate the things."
Why logs are not inserting in nohup.out file?
From info nohup:
23.4 ‘nohup’: Run a command immune to hangups
=============================================
‘nohup’ runs the given COMMAND with hangup signals ignored, so that
the command can continue running in the background after you log out.
...
If standard output is a terminal, the command’s standard output is
appended to the file ‘nohup.out’;
if that cannot be written to, [waltera: the write permission/space in current dir] it is
appended to the file ‘$HOME/nohup.out’; and if that cannot be written
to, the command is not run.
...
However, if standard output is closed, standard error terminal output
is instead appended to the file ‘nohup.out’ or ‘$HOME/nohup.out’ as
above.
When cron executes the script, stdout is not a terminal nor closed, so nohup has no reason to redirect any output.
Another demo of nohup without a terminal is when you start your script2.sh, that has an nohup command, with another nohup:
nohup ./script2.sh > masternohup.out
The output of script3.sh will be written to masternohup.out.
From man nohup
...
If standard input is a terminal, redirect it from an unreadable file.
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 it could be in $HOME/nohup.out either way it's best to use this to control output:
nohup COMMAND &> FILE
You don't need nohup in modern days.
Shell escape method allows a process to leave its process group and never receive SIGHUP nor any other signals directed to a process group.
In bash shell:
(command &>log.txt &)

Run bash script loop in background which will write result of jar command to file

I'm novice to running bash script. (you can suggest me, if title I've given is incorrect.)
I want to run a jar file using bash script in loop. Then it should write the output of jar command into some file.
Bash file datagenerate.sh
#!/bin/bash
echo Total iterations are 500
for i in {1..500}
do
the_output="$(java -jar data-generator.jar 10 1 mockData.csv data_200GB.csv)"
echo $the_output
echo Iteration $i processed
done
no_of_lines="$(wc -l data_200GB.csv)"
echo "${no_of_lines}"
I'm running above script using command nohup sh datagenerate.sh > datagenerate.log &. As I want to run this script in background, so that even I log out from ssh it should keep running & output should go into datagenerate.log.
But when I ran above command and hit enter or close the terminal it ends the process. Only Total iterations are 500 is getting logged into output file.
Let me know what I'm missing. I followed following two links to create above shell script: link-1 & link2.
nohup sh datagenerate.sh > datagenerate.log &
nohup should work this way without using screen program, but depending on your distro your sh shell might be linked to dash.
Just make your script executable:
chmod +x datagenerate.sh
and run your command like this:
nohup ./datagenerate.sh > datagenerate.log &
You should check this out:
https://linux.die.net/man/1/screen
With this programm you can close your shell while a command or script is still running. They will not be aborted and you can pick the session up again later.

Running a JMeter script with nohup

For the first time I'm just playing around with nohup on top of an Ubuntu server. I read few docs about nohup and got to know about the running commands with options such as nohup ./server.sh &.
What I want to know is that, how should I be running the JMeter script (in headless mode) using nohup? Following is the script I needed to run with nohup:
./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx
When I tried using the normal nohup operation within the script it always throws me an error saying -n command not found. How should I move on with this? Any help would be appreciated.
Although I cannot reproduce your issue you can try surrounding your command with quotation marks like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx"
Also don't forget -l key to save the results into a file.
The full command which runs script totally in the background will look like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx -l result.jtl" > /dev/null 2>&1 &
References:
nohup man page
nohup Execute Commands After You Exit From a Shell Prompt
How Do I Run JMeter in Non-GUI Mode?
Full list of JMeter command-line options

shell script : write sdterr & sdtout to file

I know this has been asked many times, but I can find a suitable answer in my case.
I croned a backup script using rsync and would like to see all output, errors or not, from the all script commands. I must write the command inside the script itself, and do not want to see output in my shell.
I have been trying with no success. Below part of the script.
#!/bin/bash
.....
BKLOG=/mnt/backup_error_$now.txt
# Log everything to log file
# something like
exec 2>&1 | tee $BKLOG
# OR
exec &> $BKLOG
I have been adding at the script beginig all kinds of exec | tee $BKLOG with adding &>, 2>&1at various part of the command line, but all failed. I either get an empty log file or incomplete. I need to see on log file what rsync has done, and the error if script failed before syncing.
Thank you for help. My shell is zsh, so any solution in zsh is welcomed.
To redirect all the stdout/stderr to a file place this line on top of your script:
BKLOG=/mnt/backup_error_$now.txt
exec &> "$BKLOG"

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

When I run nohup some_command &, the output goes to nohup.out; man nohup says to look at info nohup which in turn says:
If standard output is a terminal, the
command's standard output is appended
to the file 'nohup.out'; if that
cannot be written to, it is appended
to the file '$HOME/nohup.out'; and if
that cannot be written to, the command
is not run.
But if I already have one command using nohup with output going to /nohup.out and I want to run another, nohup command, can I redirect the output to nohup2.out?
nohup some_command &> nohup2.out &
and voila.
Older syntax for Bash version < 4:
nohup some_command > nohup2.out 2>&1 &
For some reason, the above answer did not work for me; I did not return to the command prompt after running it as I expected with the trailing &. Instead, I simply tried with
nohup some_command > nohup2.out&
and it works just as I want it to. Leaving this here in case someone else is in the same situation. Running Bash 4.3.8 for reference.
Above methods will remove your output file data whenever you run above nohup command.
To Append output in user defined file you can use >> in nohup command.
nohup your_command >> filename.out &
This command will append all output in your file without removing old data.
As the file handlers points to i-nodes (which are stored independently from file names) on Linux/Unix systems You can rename the default nohup.out to any other filename any time after starting nohup something&. So also one could do the following:
$ nohup something&
$ mv nohup.out nohup2.out
$ nohup something2&
Now something adds lines to nohup2.out and something2 to nohup.out.
my start.sh file:
#/bin/bash
nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &
There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.
I <3 forever

Resources