How to put a command in nohup in shell script? [duplicate] - bash

This question already has answers here:
How to include nohup inside a bash script?
(6 answers)
Closed 4 years ago.
I am trying to put shred command in nohup for shell script, once the script is run it has to take input and run shred operation on a device in nohup mode. The problem is when I add nohup to command the script does not exit and run the command in background, also i am trying to send success and failure mails with the output once shred operation is completed.
What I tried so far:
nohup shred -n 2 "device name" > success.out 2>failure.out
if [$? -eq 0]
then
mail -s "success" -a success.out "EmailID" <.
exit 0
else
mail -s "failure" -a failure.out "EmailID" <.
exit 1
fi
I am getting the success email with attachment n=but the attachment is non readable format, is there any other way??

"the script does not exit and run the command in background".
You do not tell the script to background it. Add &.
So:
nohup shred -n 2 "device name" & >success.out 2>failure.out
Will do it.

Related

How to run two separate bash commands at the same time, not in the same line but together Parallelly [duplicate]

how to run shell script in background in unix?
My script
#!/bin/sh
while [ true ]
do
ps -fu $USER>>/home/axway/trace.log 2>&1
sleep 10
done
running above script (shellEx1.sh) in background by nohup command on promt
nohup ./shellEX1.sh &
having below isuue:
$ nohup ./shellEX1.sh &
[3] 19520
$ nohup: ignoring input and appending output to `nohup.out'
Its warning to say like the output of the script will be written in file 'nohup.out'. In order to remove this warning, you can try
nohup ./shellEX1.sh >/tmp/output.txt &
or
nohup ./shellEX1.sh >/dev/null &
Just a thought, you could make it connect or create a screen instance at the start.
screen -S bashscript
my bash script

Run two xterm commands at the same time in BASH [duplicate]

This question already has answers here:
How do you run multiple programs in parallel from a bash script?
(19 answers)
Closed 4 years ago.
I am trying to run two different programs in xterm windows from the same automation script. My current code looks like this:
#!/bin/bash/sh
echo "STARTING PROGRAM ONE"
# change into correct directory
cd ~/myProjects/ProgramOne
xterm -e myProg1 -a P1 &> /tmp/ProgramOne/P1.txt
echo "STARTING PROGRAM TWO"
# change into correct directory
cd ~/myProjects/ProjectTwo
xterm -e myProg2 -a P2 &> /tmp/ProgramTwo/P2.txt
# Code to kill the xterm process?
echo "******************************************"
echo "START AUTOMATION COMPLETE"
echo "******************************************"
What I am looking to accomplish is to have two separate programs, in different directories, run in two different xterm windows so I can demonstrate to the end user that the programs are running appropriately.
Currently, the first program executes fine, and when I Ctrl + C it, the second kicks off just fine. However, I would like both to execute at the same time.
I have looked at a few resources on SO but have not found anything to help me with this problem.
I am on a CentOS7 system, trying to automate this process. Any help or advice would be great.
Thanks!
Start them in the background and wait for them to finish:
#!/bin/bash/sh
echo "STARTING PROGRAM ONE"
# change into correct directory
cd ~/myProjects/ProgramOne
xterm -e myProg1 -a P1 &> /tmp/ProgramOne/P1.txt &
echo "STARTING PROGRAM TWO"
# change into correct directory
cd ~/myProjects/ProjectTwo
xterm -e myProg2 -a P2 &> /tmp/ProgramTwo/P2.txt &
# Code to kill the xterm process?
wait
echo "******************************************"
echo "START AUTOMATION COMPLETE"
echo "******************************************"

shell script was not terminating

I am executing one shell script from another shell script. The included shell script is not terminating after execution. But when I run it separately, it works fine and terminates normally.
Script 1
#! /bin/bash
WebApp="R"
#----------Check for Web Application Status
localWebAppURL="http://localhost:8082/"
if curl --max-time 5 --output /dev/null --silent --head --fail "$localWebAppURL"; then
WebApp="G"
else
exec ./DownTimeCalc.sh &
fi
echo "WebApp Status|\"WebApp\":\"$WebApp\""
In above script I am calling another script called DownTimeCalc.sh.
DownTimeCalc.sh
#! /bin/bash
WebApp="R"
max=15
for (( i=1; i <= $max; ++i ))
do
if curl --max-time 5 --output /dev/null --silent --head --fail "http://localhost:8082/"; then
WebApp="G"
echo "Status|\"WebApp\":\"$WebApp\""
break
else
WebApp="R"
sleep 10
fi
echo "Status|\"WebApp\":\"$WebApp\""
done
echo "finished"
exit
exec ./DownTimeCalc.sh &
You don't need exec. If you want to run the script and wait for it to complete then just write:
./DownTimeCalc.sh
Or if you want to run it in the background and have the first script continue, write:
./DownTimeCalc.sh &
When you use & the launched process will be launched in the background and will run in the background while other commands from the foreground script run or you interact with the shell. It's doing what you told it. If you press Enter you will see any queued-up stderr output, and if you type fg it will bring the process to the foreground if it is still running.
You probably don't want to use & in this case, though.

How to use expect inside bash script [duplicate]

This question already has an answer here:
Embedding an Expect script inside a Bash script
(1 answer)
Closed 5 years ago.
Could anybody please tell me why this is not working?
#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"
It doesn't give any error but file is not transferred to remote server.I have tried many ways still couldn't find any solution.
The bash script you have defined is passing the expect commands on the standard input of expect. However, the expect command requires its arguments on a file or as an argument using the -c option.
You have several options but to add the less modifications on your script you just need to use the process substitution to create a here-document (temporary) for the expect command.
#!/bin/bash
echo "[DEBUG] INIT BASH"
cd /home
touch somefile
/usr/bin/expect <(cat << EOF
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "Password:"
send "MyPassWord\r"
interact
EOF
)
echo "[DEBUG] END BASH"

starting a job remotely with bash [duplicate]

This question already has answers here:
Getting ssh to execute a command in the background on target machine
(19 answers)
Closed 6 years ago.
I'm trying to run a bash script on a remote machine, and I'd like to return immediately after running the script in the background of the remote machine. For instance:
$ echo foo.txt
sleep 2000 &
then when I tried to do:
$ ssh x.x.x.x 'bash -s' < foo.txt
the command never returns. Is there a way to make it return while sleep runs in the background on the remote machine?
May by;
echo foo.txt
sleep 2000 >&- 2>&- <&- &
>&- means close stdout.
2>&- means close stderr.
<&- means close stdin.
& means run in the background

Resources