0

I need a small script which run three vlc command on the same time (on Ubuntu server 14.04) as stated below:

#!/bin/sh
cvlc -vvv v1 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5656,access=http}" 
cvlc -vvv v2 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5657,access=http}" 
cvlc -vvv v3 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5658,access=http}" 

I need the perfect way or method to have the script running 3 VLC commands (all commands must be running as same time and silently on back ground).

Also need another script to check if the above script is running (all vlc command on above script is running) if stopped or crashed to restart it.

Thanks in Advance

Shata

1

You can execute bash script in background using screen.

Please check out following articles on it.

https://www.howtoforge.com/linux_screen

http://www.tecmint.com/screen-command-examples-to-manage-linux-terminals/

https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/

You can use monit to keep command running. It will start if service is crashed.

0

What about try this: nohup your-command &

"nohup" ensures that process will not be terminated after logout and "&" on the end ensures that whole command will run on background.

  • Yes in this way you will execute in background the processes, but it remains the problem, later, to individuate them in a unique way. You need to store somehow their PIDs to use them later; some hints more from the aswer here – Hastur Jun 7 '15 at 11:02
0

The usual way to send in background a process is to use the & operator after your command (as suggested by Glenn).

To have the possibility to individuate the status of the process after some time you need to know its PID. So you need to find a way to capture the PID (Process ID) of process just launched.
It's available in the $! variable.

Note that you need to store it in another variable because it will be updated after each command invocation. In this case you can store the PID in a file that you can use later with a second script.
A nice place where to store it seems to be /var/lock/yourscripdirectory/lock.
At each reboot this directory should be cleaned automatically.

Your first script (let's we call it s1.sh) should execute your commands and store the PIDs and can be similar to the one below:

#!/bin/sh
MyLockDir="/var/lock/${0##*/}"
mkdir -P     # it creates if needed the directory
             # /var/lock/yourscriptname without its path 
cvlc -vvv v1 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5656,access=http}" &
echo $! >${MyLockDir}/5656  # here you store in the file 5656 the PID
cvlc -vvv v2 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5657,access=http}" &
echo $! >${MyLockDir}/5657
cvlc -vvv v3 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:5658,access=http}" &
echo $! >${MyLockDir}/5658

A second script (let's we call s2.sh) has to check if the process with the PID written in the file created before. It should checj if it is still running and to update it if needed. Thake some hints from the lines below:

#!/bin/sh
MyPort=$2
MyLockDir="/var/lock/s1.sh"
mypid="$(cat ${MyLockDir}/${MyPort})"
ps -p $mypid > /dev/null; 
if [ $? == 0 ] ; 
  then                       # Running
    echo "# It runs ... " ;  # or whatever you want
  else                       # NOT Running 
   cvlc -vvv $1 --sout "#transcode{vcodec=h264,vb=999,acodec=mp3,ab=128,deinterlace channels=2,samplerate=44100}:standard{mux=ts,dst=x.x.x.x:$MyPort,access=http}" &
   echo $! >${MyLockDir}/$MyPort
fi

e.g. you can run the second script with ./s2.sh v1 5656 (or with ./s2.sh v2 5657 ...) for each single port you need.

A slightly more complex way to do is to create for each port a directory with that name in your LockDir and put the lock file with the PID inside it, trap a remove command for the directory in the script itself and check later if the directory exists... see hints here

This way will give you the insurance that only one concurrent script can create it and run a cvlc instance on that port.


UPDATE: A comment
Q: Why to suggest to store the PID instead that to check it with other commands like ps -ef | grep '5657'?. A: In general the PID of a process is unique, meanwhile you cannot know in advance if the string you are using to grep the ps output is not used by other command... the PID exists for this reason :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.