Run a another within script - linux

I have to run another terminal within script and wait for function to execute on that terminal
before proceeding next command in shell script. Scenario is as like below
script.sh
!/bin/sh
...
...
gnome-terminal --working-directory=#{image_path} -e ./test.sh # execute test.sh script on another terminal
./switch 0 0 # I have to execute this command after test.sh script gets completed on another terminal
...
...
Here problem is. test.sh script is not gets executed fully and returned back immediately to parent script and executes ./switch 0 0 command. As this ./switch 0 0 command depends on test.sh script completion, my script gets failed.
Regards
Manish B.

I'm not sure if there's a switch in gnome-terminal that can make it run synchronously, but you can use xterm for that. Xterm won't go to the background unlike other terminals:
( D=$(pwd); cd "$image_path"; xterm -e "$D/test.sh" )
./switch 0 0

Related

How to notify another shell script if a running shell script is stopped?

I run a shell script say script 1. I pause the execution of script 1 and give the bg command so it starts running in background. I start another script 2 which is again paused and made to run in the background. Now I give the pkill -f script1.sh command to kill the script 1.
What I want to do is that when I kill the script 1 , the script 2 should come to know about this and then script 2 should start running another script 3. How can this be done ?
There are different ways of doing this. The most simple is:
script1
^z
bg
script2 $!
And in script2 at some strategic points:
s1pid=$1
...
if ps $s1pid ; then
script3
fi
...
You can also use a PID file. This works good if there is only a single instance of script1 running system-wide, or if you do some tricks in naming or placing the PID file (f.e. put the PID file in the current directory). You can use the PID in the PID file to test if the script1 still runs.
If you are the only one that runs this script, and you can name it anything you want, you can give it perhaps some unique name. In that case, you can
if ps -ef | grep some_unique_name ; then
script3
fi
You can also lock the PID file as #jhnc suggested with flock.
If you do not have strategic points in script2 where you can poll for script1, you might get some sub-process to poll it in the background.

How to keep a bash script running in the background

I write a simple bash script:
while :
do
sleep 2;
//my code
done
Now I want this bash script always be running.
bash mybash.sh > /dev/null &
When I run above command my bash works fine. but when I close my terminal I think my bash is killed. because it doesn't work as my script make some files when it running.
Run the script "bash script.sh" in terminal and press ctrl+z and then use 'bg' command to put the script in background
#!/bin/bash
while true; do
// your code
sleep 5;
done;
write a bash script and put it that to cron and check once it will start comment the cron it will run in a background.
insted of sleep 5 you can use whatever second you want to put.
For checking your process use below commend to get the details
ps -ef | grep script_file_name
if you find more then one process is running leave one process and rest kill the process for script.
Hope so this will resolve your issue....!!!!

How Can I Run an Infinite Loop from a Bash Script with Output to Foreground

So, I want to run the below command from a bash script and have it output to the shell, however, all my attempts result in the script running in the background:
while [ 1 ]; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap"; sleep 1; done
When I run the command directly in the shell prompt, it outputs as expected. The application, SNGREP, launches with the specified parameters and works well.
I have experimented with sending the command to Screen, but it still ends up in the background. I have also tried modifying the command by sleeping first (as follows):
while sleep 1; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap"; done
It, too, goes to the background but then runs fine if I type it directly into the shell prompt. What else can I try to get the command to output to the foreground when run from a bash script? Any help is appreciated, thanks.
PS. My end goal is to launch SNGREP in a Putty window from a Windows Batch File. I've got everything working, but this last bit.
It is not clear from your command why it is running in background, rather it should run on foreground only. You can try with below, redirect(2>&1) the output to standard output always:-
while [ 1 ]; do timeout -k9 21600 sngrep -c -O "/var/log/sngrep/sngrep_capture_$(date +%F-%H-%M-%S).pcap" 2>&1; sleep 1; done

Run a cron job now

I have a shell script called import.sh . This script will be used only once and will run for atleast 2 days.
I am able to schedule a cronjob like below.
02 10 25 7 * while IFS=',' read a;do /home/$USER/import.sh $a;done < /home/$USER/input/xaa
input.sh is the shell script
xaa is the file that contains arguments.
Now I want to run this script now.
I have tried ./import.sh xaa and sh -x import.sh xaa but If I run them in a terminal then I have to leave the terminal open for the time the script runs which might take more than 2 days.
How can I schedule the job to run now and terminate as soon as it finishes.
When using the command line interface for Linux, prefixing any command with nohup prevents the command from being aborted if you log out or exit the command line interface.
So you can do something like below.
nohup ./import.sh xaa

Script command losing alias from shell

When I run the script command it loses all the aliases from the existing shell which is not desired for people using lots of aliases. So, I am trying to see if I can automatically source the .profile again to see if it works without the user have to do it.
Here below is the code:
#!/bin/bash -l
rm aliaspipe
mkfifo aliaspipe
bash -c "sleep 1;echo 'source ~/.bash_profile' > aliaspipe ;echo 'alias' > aliaspipe ;echo 'exec 0<&-' > aliaspipe"&
echo "starting script for recording"
script < aliaspipe
Basically I am creating a named pipe and the making the pipe as stdin to the script program, trying to run the source command and then close the stdin from pipe to the terminal stdin so that I can continue with the script.
But when I execute, the script is exiting after I execute "exec 0<&-",
bash-3.2$ exec 0<&-
bash-3.2$ exit
Script done, output file is typescript
Not sure why the exit is called and script is terminated. If I can make the script move the stdin from pipe to terminal then it should be fine.
You can get script to execute a bash login shell by telling it to do so explicitly.
# Gnu script (most Linux distros)
script -c "bash -l"
# BSD script (also Mac OS X)
script typescript bash -l
That will cause your .bash_profile to be sourced.
By the way, redirections are not stacks. When you write exec 0<&-, you're closing standard input, and when bash's standard input is closed, it exits.

Resources