Unix - ps to check if script running
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.
I did some research on here and figured this to be a working code for my functionality as described above.
#!/bin/bash
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;
I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.
process solaris
add a comment |Â
up vote
0
down vote
favorite
I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.
I did some research on here and figured this to be a working code for my functionality as described above.
#!/bin/bash
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;
I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.
process solaris
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance withCTRL-C
or similar. And you'll also likely have to deal with cleaning up after users eitherkill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.
â Andrew Henle
May 8 at 9:48
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.
I did some research on here and figured this to be a working code for my functionality as described above.
#!/bin/bash
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;
I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.
process solaris
I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.
I did some research on here and figured this to be a working code for my functionality as described above.
#!/bin/bash
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;
I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.
process solaris
edited May 8 at 10:03
Jeff Schaller
31.1k846105
31.1k846105
asked May 8 at 9:05
ratcat
12
12
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance withCTRL-C
or similar. And you'll also likely have to deal with cleaning up after users eitherkill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.
â Andrew Henle
May 8 at 9:48
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03
add a comment |Â
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance withCTRL-C
or similar. And you'll also likely have to deal with cleaning up after users eitherkill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.
â Andrew Henle
May 8 at 9:48
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with
CTRL-C
or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.â Andrew Henle
May 8 at 9:48
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with
CTRL-C
or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.â Andrew Henle
May 8 at 9:48
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
The ps command you are using is not related to UNIX.
Using key letters
instead of options
was used in BSD ps
but that did never make it into the standard. BSD even uses the h
key letter in a different way as you might expect.
I recommend to use:
ps -p $$
If you like to check whether there are more instances of this script, use:
myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids
In theory, you do not even need to run ps
anymore, it would be sufficient to check the output of pgrep
for a comma.
If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:
set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript
Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.
2
Saying to useps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.
â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The ps command you are using is not related to UNIX.
Using key letters
instead of options
was used in BSD ps
but that did never make it into the standard. BSD even uses the h
key letter in a different way as you might expect.
I recommend to use:
ps -p $$
If you like to check whether there are more instances of this script, use:
myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids
In theory, you do not even need to run ps
anymore, it would be sufficient to check the output of pgrep
for a comma.
If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:
set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript
Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.
2
Saying to useps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.
â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
add a comment |Â
up vote
0
down vote
The ps command you are using is not related to UNIX.
Using key letters
instead of options
was used in BSD ps
but that did never make it into the standard. BSD even uses the h
key letter in a different way as you might expect.
I recommend to use:
ps -p $$
If you like to check whether there are more instances of this script, use:
myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids
In theory, you do not even need to run ps
anymore, it would be sufficient to check the output of pgrep
for a comma.
If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:
set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript
Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.
2
Saying to useps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.
â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The ps command you are using is not related to UNIX.
Using key letters
instead of options
was used in BSD ps
but that did never make it into the standard. BSD even uses the h
key letter in a different way as you might expect.
I recommend to use:
ps -p $$
If you like to check whether there are more instances of this script, use:
myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids
In theory, you do not even need to run ps
anymore, it would be sufficient to check the output of pgrep
for a comma.
If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:
set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript
Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.
The ps command you are using is not related to UNIX.
Using key letters
instead of options
was used in BSD ps
but that did never make it into the standard. BSD even uses the h
key letter in a different way as you might expect.
I recommend to use:
ps -p $$
If you like to check whether there are more instances of this script, use:
myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids
In theory, you do not even need to run ps
anymore, it would be sufficient to check the output of pgrep
for a comma.
If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:
set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript
Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.
edited May 9 at 10:29
answered May 8 at 9:36
schily
8,66231435
8,66231435
2
Saying to useps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.
â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
add a comment |Â
2
Saying to useps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.
â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
2
2
Saying to use
ps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.â Jeff Schaller
May 8 at 10:04
Saying to use
ps -p $$
means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.â Jeff Schaller
May 8 at 10:04
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.
â ratcat
May 9 at 0:55
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
So my updated answer should be sufficient for you...
â schily
May 9 at 10:27
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442493%2funix-ps-to-check-if-script-running%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.
â amisax
May 8 at 9:12
If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with
CTRL-C
or similar. And you'll also likely have to deal with cleaning up after users eitherkill -9 ...
a running script or remove the lock file themselves and try running another instance of the script.â Andrew Henle
May 8 at 9:48
I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew
â ratcat
May 9 at 7:03