How to check if a file is open in editor?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to check if a file is open in editor?
# 8  
Old 10-17-2017
Quote:
Originally Posted by cherryTango
For the fuser solution not sure I understand what is meant by the

Any further information would be great! Thank you!
When you open a file using vim it creates a temporary .swp file, hence I included it.

But as Bakunin mentioned, fuser will not cover all file editors since some editors works differently.
# 9  
Old 10-17-2017
Not quite clear what exactly you are doing there; please give more info. Using leafpad to open / edit a file:
Code:
pgrep -xf "leafpad file1"; echo $?
6200
0
pgrep -xf "leafpad file1"; echo $?
1

First is with open file, second after closing it.
# 10  
Old 10-17-2017
So I've got a program that allows the user to edit a file either in gedit or using vim. I want a log to update only once the user has finished editing the file. Figured easiest way to do this would be only to update once the user had closed the editor. Been playing around with it for a while now - think I have found the best solution for what I want to do however I can't seem to get it to work.

My code:
Code:
gedit "$1" & PID="$!"
            # Do stuff while file is open
            wait $PID
            clear
            updateLog

(For the vim option the code is identical except 'vim' replaces 'gedit')

My logic is that this will guarantee that the process is terminated before updating the log.. however in practice it just immediately updates the log. Have absolutely no idea what I'm doing wrong! The $PID variable is being filled correctly as I've tested by printing it. Does the PID for a process change and this is why it's not working? Or is my wait command incorrect? When I put an
Code:
exit

command after the wait there are no errors in the terminal.
# 11  
Old 10-18-2017
Have you tried just wait rather than wait $PID? Or how about not putting it into the background at all:

Code:
gedit "$1"
echo "Editing now finished"

# 12  
Old 10-18-2017
A process ID does not change during the life of a process. But with a clear command in your code, any diagnostics messages that might be printed to give you an indication of what went wrong will likely be erased before you get a chance to see them.

How is it that you expect to edit a file when your editor is running in the background and can't read editing instructions from the terminal?
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 10-18-2017
Hi again, still struggling with this. All the solutions I see online seem to be working for everyone else but not me! They always return that the process is active initially and then return that it is not - even if the editor is still open. For example:
Code:
gedit "$1" & PID="$!"
            echo "$PID"
            
                me="$(basename $0)($$):"
                if [ -z "$PID" ]
                then
                    echo "$me a PID is required as an argument" >&2
                    exit 2
                fi

                name=$(ps -p $PID -o comm=)
                if [ $? -eq 0 ]
                then
                    echo "$me waiting for PID $PID to finish ($name)"
                    while ps -p $PID > /dev/null; do echo "Running"; sleep 1; done;
                else
                    echo "$me failed to find process with PID $PID" >&2
                    exit 1
                fi
            echo "Finished."

Will return:
Code:
20946
ex_03.sh(20854): waiting for PID 20946 to finish (gedit)
Running
Finished.

Another example is:
Code:
gedit "$1" & PID="$!"
            echo "$PID"
            (while kill -0 $PID; do echo "Running"; sleep 1; done) && echo "Finished."

Will return:
Code:
21078
Running
./ex_03.sh: line 446: kill: (21078) - No such process
Finished.

Finally:
Code:
gedit "$1" & PID="$!"
            echo "$PID"
            echo "Running"
            wait $PID
            echo "Finished."

Will return:
Code:
21190
Running
Finished.

In all cases I have not closed the editor (I don't even have time to before the script stops running). In the final example I attempted to give it some random PID that I just typed in to see if it returned an error message - which it did in several cases and it returned various different error messages - but not from the variable I give it. I am totally running out of ideas here. Is it possible that the PID changes?

---------- Post updated at 04:29 PM ---------- Previous update was at 04:10 PM ----------

Update: Also, when trying this:
Code:
gedit "$1" 
            PID=`ps -ef | grep ${USER} | grep "gedit $1" | grep -v grep | awk '{ print $2 }'`
            echo "$PID"
            while : 
            do
            sleep 1
                if [ -x /proc/$PID ]; then
                    echo "Running"
                else
                    echo "Finished."
                fi
            done

It returns:
Code:
Running
Running
Running
###Keeps running...

No PID and continues to run. When I try this:
Code:
gedit "$1" & PID="$!"
            echo "$PID"
            while : 
            do
            sleep 1
                if [ -x /proc/$PID ]; then
                    echo "Running"
                else
                    echo "Finished."
                fi
            done

It returns:
Code:
22729
Finished.
Finished.
Finished.
###Keeps saying 'finished'..

However this time it does get the PID. I've seen examples for the first scenario identical to mine where it does get the PID. Any further help would be so greatly appreciated!
# 14  
Old 10-18-2017
Have you tried my suggestion of not putting gedit in the background at all?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

2. Shell Programming and Scripting

Not able to read .csv file until open in vi editor

Hi, I am facing a problem regarding .csv file, my script does not read .csv file and if i open this file in vi editor and perform :wq option then only my script reads the .csv file. Thanks (5 Replies)
Discussion started by: ranabhavish
5 Replies

3. UNIX for Dummies Questions & Answers

Vi editor will not open new to UNIX. help please.

vi: syntax error at line 1: `)' unexpected when I try to vi into the /etc/vfstab, the return gives me the above error. how can resolve this so that I can have access into vi. (6 Replies)
Discussion started by: dovestar
6 Replies

4. UNIX and Linux Applications

Notepad++ hang when open file edited in other text editor

Hi, I would like to ask about the notepad++ text editor application, Although there are alternative and more great text editor in linux (gedit, geany, jedit) im still using the notepad++ sometimes cause for some of my own reason one of those is the minimalist text(what i mean is notepad++ has a... (2 Replies)
Discussion started by: jao_madn
2 Replies

5. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

6. Shell Programming and Scripting

avoid open file to check field.

Hi Everyone, # cat a.txt 94,aqqc,62345907, 5,aeec,77, # cat 1.pl #!/usr/bin/perl use strict; use warnings; use Date::Manip; open(my $FA, "/root/a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\,/, $_); if (index($tmp, "qq") ne -1) { ... (4 Replies)
Discussion started by: jimmy_y
4 Replies

7. UNIX for Dummies Questions & Answers

How to open file in VI Editor at a specific line?

i have following query e.g i want the VI Editor cursor at line number N instead of 0 while opening the file from unix prompt. vi filename ?????? Can anyone help? (4 Replies)
Discussion started by: skyineyes
4 Replies

8. UNIX for Advanced & Expert Users

getting error when open vi editor

helo I install my product for koren language I m uisng RHEL -4 operating system now problem is whenever I open any file vi filename I got following error on the screen E557: Cannot open termcap file 'vt100' not known. Available builtin terminals are: builtin_ansi ... (1 Reply)
Discussion started by: amitpansuria
1 Replies

9. Shell Programming and Scripting

How to give permissions to an open file in vi editor?

Hi all, I have a shell script that i started editing, only in the midst of which i tried to save the changes i found that the file wasnt been provided with write/execute permissions. I later have redone the changes and saved the file- Just curious to know if there was any command wherein... (5 Replies)
Discussion started by: Pankajakshan
5 Replies

10. UNIX for Advanced & Expert Users

Unable to open files in VI editor

Hi, I unable to open any file using Vi editor,constantly getting the following error, vi log.log *** glibc detected *** malloc(): memory corruption: 0x081af510 ** uname -a Linux sekac092 2.6.5-7.244-bigsmp #1 SMP Mon Dec 12 18:32:25 UTC 2005 i686 athlon i386 GNU/Linux Also Note ls... (8 Replies)
Discussion started by: ennstate
8 Replies
Login or Register to Ask a Question