Script to kill a process running at a certain port


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to kill a process running at a certain port
# 1  
Old 06-22-2014
Script to kill a process running at a certain port

Hello,
I have multiple scripts (vlc1, vlc2,...vlc5) and as I do not know how to run them as upstart processes, I entered my script links into rc.local file.
Here is the sample one for process vlc1:

Code:
$ nano /etc/rc.local

added below line into rc.local
Code:
/var/bin/./vlc1 &

Port nr of vlc1 process is 14001

When I want to kill vlc1, i enter:
Code:
$ sudo netstat -lpn |grep :14001

it gives related process number, let's assume that it's 950. I type,
Code:
$ kill 950

Can anybody help me on how to run a script to kill vlc1 or how I can run vlc1 with restart/stop/start commands in terminal ?

Thanks in advance
Boris

Last edited by Scrutinizer; 06-22-2014 at 12:43 PM.. Reason: code tags
# 2  
Old 06-22-2014
Firstly, do you need different scripts files for each - are these all essentially the same script, with just the port number replaced in each?

If so we could consider writing a vlcN script, where the port number is passed into the script e.g:

Code:
/var/bin/vlcN -p 14001 stop
/var/bin/vlcN -p 14001-14010 start
/var/bin/vlcN -p 14005 restart

# 3  
Old 06-23-2014
Quote:
Originally Posted by Chubler_XL
Firstly, do you need different scripts files for each - are these all essentially the same script, with just the port number replaced in each?

If so we could consider writing a vlcN script, where the port number is passed into the script e.g:

Code:
/var/bin/vlcN -p 14001 stop
/var/bin/vlcN -p 14001-14010 start
/var/bin/vlcN -p 14005 restart

Hello,

Seperate script for each process would be better.
So if you would share only one script, I can create for others..

Some softwares have start/stop/restart functions and you can control the software status from terminal line. If you need to know VLC code, I can share it under this thread.


Thanks in advance
Boris
# 4  
Old 06-23-2014
This is a start
Code:
port=14001
port_to_pid(){
netstat -lpn | awk '$0~":"p"\\>" {x=$NF; sub(/^[^0-9]*/,"",x); sub(/[^0-9]*$/,"",x); print x; exit}' p="$1"
}

case $1 in
start)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    echo "already running, pid=$pid"
  else
    ...
  fi
;;
stop)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    kill "$pid"
  fi
;;
restart) $0 stop; sleep 1; $0 start
;;
esac

# 5  
Old 06-23-2014
Quote:
Originally Posted by MadeInGermany
This is a start
Code:
port=14001
port_to_pid(){
netstat -lpn | awk '$0~":"p"\\>" {x=$NF; sub(/^[^0-9]*/,"",x); sub(/[^0-9]*$/,"",x); print x; exit}' p="$1"
}

case $1 in
start)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    echo "already running, pid=$pid"
  else
    ...
  fi
;;
stop)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    kill "$pid"
  fi
;;
restart) $0 stop; sleep 1; $0 start
;;
esac

Hello MadeInGermany,
Sorry but, how pid nr will understand that vlc1 should be restarted when i run this code?There is no "vlc1" expression in this code. Nicht functioniert.

Boris
# 6  
Old 06-23-2014
As said, this is a start.
E.g. replace the ... by the "vlc1" command...
and make the new script executable...
# 7  
Old 06-23-2014
Hello,
Here is the latest script file:

filename: sc1
Code:
port=14001
port_to_pid(){
netstat -lpn | awk '$0~":"p"\\>" {x=$NF; sub(/^[^0-9]*/,"",x); sub(/[^0-9]*$/,"",x); print x; exit}' p="$1"
}

case $1 in
start)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    echo "already running, pid=$pid"
  else
    sudo -u boris cvlc -vvv /home/boris2/movie.avi --sout '#transcode{vcodec=mp2v,acodec=a52,vb=1024,ab=64,width=704,height=480,deinterlace}: standard{access=http,mux=ts,dst=computer_dynamic_ip:14001}' :sout-mux-caching=10000 > video_log 2>&1 & 
  fi
;;
stop)
  pid=`port_to_pid $port`
  if [ -n "$pid" ]; then
    kill "$pid"
  fi
;;
restart) $0 stop; sleep 1; $0 start
;;
esac

Do you think that anything I did wrong?
When i listen 14001, no pid result comes out.
I also did:

Code:
$ chmod 755 sc1

Thanks in advance
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Idle command to kill the process running n unx box machine

Hi Team , I have one process named as cec_analysiseool that is running on unix box machine now i want to kill this process so please advise what will be the ideal command to kill this , what i have tried is :confused: kill -9 `ps -ef | grep cec_analysiseool | grep -v grep | awk '{print $2}'` (2 Replies)
Discussion started by: unclesamm
2 Replies

2. Shell Programming and Scripting

Command to know all the Current running process and how to kill

All, 1.What is the unix comand used for all current running process (Including All current running processes Parent ->child->subchild process) 2.If child and subchild processes are running then what is the unix command to kill parent and its all child subchild processes in UNIX. Kindly... (7 Replies)
Discussion started by: skp
7 Replies

3. Shell Programming and Scripting

Help with kill a specific process after certain running time

Hi, Do anybody experience to write a bash script in order to kill a specific process (java) after certain time of running? eg. java java.jar task_run.txt I will run a java program (java.jar) which will run a long list of process (task_run.txt) one by one. I plan to terminate the java... (5 Replies)
Discussion started by: perl_beginner
5 Replies

4. AIX

Process running on which port

I would like to know, how to find that particular process is running on which port other than /etc/services as this file shows well known ports information Double post, continued here. If you want answers for different OS, post in a general section instead. If you want your other post moved... (0 Replies)
Discussion started by: amity
0 Replies

5. Solaris

Process running on which port

Hi I would like to know, how to find that particular process is running on which port other than /etc/services as this file shows well known ports information. (1 Reply)
Discussion started by: amity
1 Replies

6. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

7. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

8. UNIX for Dummies Questions & Answers

See if process is running and kill it

I need a simple line to run from apple remote desktop's Unix terminal on multiple different machines to see if Microsoft Entourage is running and kill it.. I imagine this could be done with a ps auxwww and grab the second field, (PID) put it into a variable and do a kill $variable using awk, but... (5 Replies)
Discussion started by: glev2005
5 Replies

9. UNIX for Advanced & Expert Users

finding a process running on a port

how do I find the process that is running on a port in HP-Unix? Murali (8 Replies)
Discussion started by: manduva
8 Replies

10. UNIX for Dummies Questions & Answers

how to kill process while keeping the submitted job running

:confused: I have a process which was schedule to run from 8am - 6pm daily. The scripts will search & run the commands that i was predefined in the database. Ususally, there were about 6-7 jobs for each submission and each job takes about 1-2 hrs and running one by one. And, I have a cron job... (3 Replies)
Discussion started by: hk_newbie
3 Replies
Login or Register to Ask a Question