Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How to check if a file is open in editor?

UNIX for Beginners Questions & Answers


Tags
bash, check, file, open, unix

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 10-17-2017   -   Original Discussion by cherryTango
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 November 2017, 12:39 AM EST
Location: Galactic Empire
Posts: 3,603
Thanks: 253
Thanked 1,316 Times in 1,233 Posts
Quote:
Originally Posted by cherryTango View Post
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.
Sponsored Links
    #9  
Old Unix and Linux 10-17-2017   -   Original Discussion by cherryTango
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,619 Times in 3,324 Posts
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.
Sponsored Links
    #10  
Old Unix and Linux 10-17-2017   -   Original Discussion by cherryTango
cherryTango cherryTango is offline
Registered User
 
Join Date: Oct 2017
Last Activity: 21 October 2017, 6:23 AM EDT
Location: Scotland
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
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 Unix and Linux 10-18-2017   -   Original Discussion by cherryTango
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2017, 5:53 PM EST
Location: Saskatchewan
Posts: 22,519
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
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"

Sponsored Links
    #12  
Old Unix and Linux 10-18-2017   -   Original Discussion by cherryTango
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 2:02 AM EST
Location: San Jose, CA, USA
Posts: 10,675
Thanks: 572
Thanked 3,738 Times in 3,190 Posts
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?
The Following User Says Thank You to Don Cragun For This Useful Post:
Corona688 (10-18-2017)
Sponsored Links
    #13  
Old Unix and Linux 10-18-2017   -   Original Discussion by cherryTango
cherryTango cherryTango is offline
Registered User
 
Join Date: Oct 2017
Last Activity: 21 October 2017, 6:23 AM EDT
Location: Scotland
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #14  
Old Unix and Linux 10-18-2017   -   Original Discussion by cherryTango
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2017, 5:53 PM EST
Location: Saskatchewan
Posts: 22,519
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
Have you tried my suggestion of not putting gedit in the background at all?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Not able to read .csv file until open in vi editor ranabhavish Shell Programming and Scripting 5 11-23-2013 06:06 AM
Notepad++ hang when open file edited in other text editor jao_madn UNIX and Linux Applications 2 04-22-2012 12:26 PM
Before I delete any file in Unix, How can I check no open file handle is pointing to that file? kchinnam Solaris 12 10-06-2010 03:30 PM
How to open file in VI Editor at a specific line? skyineyes UNIX for Dummies Questions & Answers 4 11-14-2008 07:36 AM
How to give permissions to an open file in vi editor? Pankajakshan Shell Programming and Scripting 5 07-08-2008 05:56 AM



All times are GMT -4. The time now is 03:36 AM.