Using grep command to detect presence of script run

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep command to detect presence of script run
# 1  
Old 04-01-2014
Using grep command to detect presence of script run

i have this line of code on a korn shell script to detect the presence of script run:

ISRUNNING=`ps -eaf -o args | grep -i sfs_load_file.ksh | grep -v grep | wc -l`

sometimes this returns either 1, 2, or 3. when it returns 2 or 3 that tells us that there are more than 1 script of sfs_load_file.ksh runs at the same time. when in reality there is none. i suppose that when i run the sfs_load_file.ksh the variable $ISRUNNING should return only 1 that is because there are none other sfs_load_file.ksh is running.

please help and advise. the code might need some tweak or corrections.

thanks so much,

Last edited by wtolentino; 04-01-2014 at 02:59 PM..
# 2  
Old 04-01-2014
You can also fuser the script file, which even detects peopel who source the script ('bash <script' or '. script').
# 3  
Old 04-01-2014
Actually it should return 0 if there are 0 processes.
Try this one:
ps -eo args | grep -ic "sfs_load_file[.]ksh"

BTW one can supress the ps header:
ps -eo args= | grep -ic "sfs_load_file[.]ksh"

Last edited by MadeInGermany; 04-02-2014 at 03:38 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 04-01-2014
Case sensitivity is OK, so 'grep -c' not grep -ic'. The meta characters '[]' escaping the '.' keep it from being counted. LINUX users have 'pgrep -c sfs_load_file.ksh'

Still misses sourced runs, or someone running it through a link or sym-link. The fuser tactic is slower, but gets everyone on the inode. That just leaves people running it through a copy of a different name.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 04-01-2014
that works thanks so much
# 6  
Old 04-01-2014
Even in pgrep it makes sense to escape the "wildcard dot":
pgrep -c "sfs_load_file[.]ksh"

# 7  
Old 04-01-2014
The standard way is to use a PID file. When you run the script, save its PID in a file. That way you can check
if [ -s pidfile ] && ps `cat pidfile` >/dev/null
        echo running
        echo not running

Much nicer than the ps | awk | sed | cut | kitchen | sink nonsense.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to detect which process is run by what?

Hello, I am running under ubuntu 14.04 Very long time ago, I set a script ( to block ip addresses abusing my system which was not active. I have not touched the server over six months or more. Today, after restart the system, started running all of a sudden and keep submitting... (4 Replies)
Discussion started by: baris35
4 Replies

2. Ubuntu

Detect and run command upon mouse movement

I am trying to find a way to run a command upon any movement of a mouse. The 1st statement shows the mouse co-ordinates. So it can detect mouse movement. xinput test 9 First list input devices: $ xinput list If possible, I would like to use it in a bash script. (2 Replies)
Discussion started by: drew77
2 Replies

3. Shell Programming and Scripting

Substitute grep command at run time

HI I am trying to use the following code in the shell script (using grep) usage() { echo "Usage: ./$0 <file name> <interval> <pattern>" } METRICS_FILE=$1 INTERVAL=$2 PATTERN="$3" .. if then PATTERN="grep Gx" fi COUNT=`cat ${METRICS_FILE} | "${PATTERN}" |egrep... (8 Replies)
Discussion started by: asifansari
8 Replies

4. Shell Programming and Scripting

Run script like command

hello i have write a script which can create username + password #!/bin/bash # Script to add a user to Linux system if ; then read -p "Enter username : " username read -s -p "Enter password : " password egrep "^$username" /etc/passwd >/dev/null if ; then... (3 Replies)
Discussion started by: nimafire
3 Replies

5. Shell Programming and Scripting

Script to run command one by one

Hi, I have run a lot of commands one after one, but I can't run them simultaneous. One command has to run and when finished second command has to run etc. Also I would like to save all the outputs created by the commands in a file. The commands can sometimes take hours, so it is also... (10 Replies)
Discussion started by: misterx12345
10 Replies

6. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

7. Shell Programming and Scripting

script will not run cp command

Hi, Not sure what the issue is here, but when i run the script. A simple copy command, it does not find the cp command ? See scrpt below : #!/bin/sh set -x ############################################# # Backup Processes #... (4 Replies)
Discussion started by: venhart
4 Replies

8. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi "" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

9. OS X (Apple)

command to check presence of volume

Hi: I'm not really a programmer, but I've created a small logout hook script to copy some specific directories to a server volume when a user logs out (10.6.4). These is a laptop user, so I'm looking find: 1) the command to check if the volume (including path to directory?) is available. ... (1 Reply)
Discussion started by: kevinmmac
1 Replies

10. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies
Login or Register to Ask a Question