Sponsored Content
Top Forums Shell Programming and Scripting Script function which checks if itself is already running Post 302821973 by fpmurphy on Sunday 16th of June 2013 11:47:44 AM
Old 06-16-2013
Rather than use /var/tmp, the more common place to put lock files is in /var/run.

You also need a trap statement to remove the lock if somebody or something terminates the script.
Code:
LOCKFILE=/var/run/script.lock

trap "{ rm -f $LOCKFILE; exit 255; }" EXIT

if  [ -f $LOCKFILE ]
then
   echo "Already running. Exiting."
   exit 1
fi

touch $LOCKFILE

<main script code>

exit 0

This User Gave Thanks to fpmurphy For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that checks for previous instances running

Hello, I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my... (5 Replies)
Discussion started by: bd_joy
5 Replies

2. Shell Programming and Scripting

Want to make a script that checks connection protocol

Hello all, I currently connect to several servers multiple times a day. Most of the time I connect via SSH through the terminal emulator poderosa (my personal favorite), but sometimes I connect via telnet through xstart because I need it to export a GUI. What I want to do is add something to... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

3. Shell Programming and Scripting

Running function or command concurrently in a bash script

I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,... (8 Replies)
Discussion started by: dj_bridges
8 Replies

4. Shell Programming and Scripting

Script stops running the remaining checks after becoming admin

Hi all, I encountered a problem where my script stops running the remaining checks after becoming an admin that is written within the script. For example: ========================================= #!/bin/sh check 1 # Runs successfully check 2 # Runs successfully /com/bin/admin #... (1 Reply)
Discussion started by: seanchew
1 Replies

5. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

6. Shell Programming and Scripting

[Bash] MD5 Checks with Script.

Hi. I'm triyng to make a Bash Script that checks (recursively) the MD5 from all the files in a certain directory and compare them against some other check that should be already done and saved in a file. I've reached to the point where i have the MD5 from the file and the MD5 that the script... (1 Reply)
Discussion started by: BiFo
1 Replies

7. Shell Programming and Scripting

Function not running in script.

I an using the below functions in my script. --------- checkRT() { subHeader "Runtime Check" for nn in `cat $HOST_FILE|egrep -i 'msdp|ca|backup|db'|grep -v '#'|sed '/^$/d'|awk '{ print $1":"$2":"$3}'` do fhba=`expr $nn|cut -d: -f2` fhba2=`expr $nn|cut -d: -f3` subSubHeader $fhba2 ssh... (1 Reply)
Discussion started by: nandan8a
1 Replies

8. Shell Programming and Scripting

Script to performs checks

Hi , I need a script which performs below activity I have one file named "testfile" in 9 different directories with same name. I want to perform below action with each testfile of each directory. if ; then mv listfiles listfiles_`date +%b%y` else echo No Such files fi ... (4 Replies)
Discussion started by: sv0081493
4 Replies

9. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

10. Shell Programming and Scripting

Help with delaying script and implementing checks before completion

Hello everyone, I was wondering if any of you could help me with this. I am an absolute beginner and don't know how to program, but I can follow a tutorial and tweak code sometimes. My understanding of programing is limitted to what for and while loops do, and how if then else logic works. That... (2 Replies)
Discussion started by: tomeurp
2 Replies
LOCKFILE(3)						     Linux Programmer's Manual						       LOCKFILE(3)

NAME
flockfile, ftrylockfile, funlockfile - lock FILE for stdio SYNOPSIS
#include <stdio.h> void flockfile(FILE *filehandle); int ftrylockfile(FILE *filehandle); void funlockfile(FILE *filehandle); DESCRIPTION
The stdio functions are thread-safe. This is achieved by assigning to each FILE object a lockcount and (if the lockcount is nonzero) an owning thread. For each library call, these functions wait until the FILE object is no longer locked by a different thread, then lock it, do the requested I/O, and unlock the object again. (Note: this locking has nothing to do with the file locking done by functions like flock(2) and lockf(3).) All this is invisible to the C-programmer, but there may be two reasons to wish for more detailed control. On the one hand, maybe a series of I/O actions by one thread belongs together, and should not be interrupted by the I/O of some other thread. On the other hand, maybe the locking overhead should be avoided for greater efficiency. To this end, a thread can explicitly lock the FILE object, then do its series of I/O actions, then unlock. This prevents other threads from coming in between. If the reason for doing this was to achieve greater efficiency, one does the I/O with the non-locking versions of the stdio functions: with getc_unlocked() and putc_unlocked() instead of getc() and putc(). The flockfile() function waits for *filehandle to be no longer locked by a different thread, then makes the current thread owner of *file- handle, and increments the lockcount. The funlockfile() function decrements the lock count. The ftrylockfile() function is a non-blocking version of flockfile(). It does nothing in case some other thread owns *filehandle, and it obtains ownership and increments the lockcount otherwise. RETURN VALUE
The ftrylockfile() function returns zero for success (the lock was obtained), and nonzero for failure. ERRORS
None. AVAILABILITY
These functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is defined. They are in libc since libc 5.1.1 and in glibc since glibc 2.0. CONFORMING TO
POSIX.1 SEE ALSO
unlocked_stdio(3) 2001-10-18 LOCKFILE(3)
All times are GMT -4. The time now is 06:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy