LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-15-2013, 10:44 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
Synchronize a script with another software in linux


I want a script starts to run in background automatically each time google-chrome started and automatically turn off itself whenever google-chrome closed. Is there a simple way to do it under linux? I would like to see some details about how it can be done. Thank you for the answers.
 
Old 10-15-2013, 11:09 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,592

Rep: Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559Reputation: 2559
Quote:
Originally Posted by cristalp View Post
I want a script starts to run in background automatically each time google-chrome started and automatically turn off itself whenever google-chrome closed. Is there a simple way to do it under linux? I would like to see some details about how it can be done. Thank you for the answers.
Then write a script to run whatever you want first, start Chrome, then after Chrome exits, the script will continue, and run whatever else you want. So:
Code:
#!/bin/bash
<whatever command(s) you want>
/usr/bin/google-chrome
<whatever other commands you want>
...and modify your desktop shortcut to call your script. Or, since the /usr/bin/google-chrome program is a script itself...put whatever you want in IT.

Last edited by TB0ne; 10-15-2013 at 11:10 AM.
 
1 members found this post helpful.
Old 10-15-2013, 02:12 PM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Then write a script to run whatever you want first, start Chrome, then after Chrome exits, the script will continue, and run whatever else you want. So:
Code:
#!/bin/bash
<whatever command(s) you want>
/usr/bin/google-chrome
<whatever other commands you want>
...and modify your desktop shortcut to call your script. Or, since the /usr/bin/google-chrome program is a script itself...put whatever you want in IT.
Thanks for your answer. I have tried it. However, it does not work for me. Actually, my script did the job to automatically backup my chrome bookmarks to my Dropbox directory whenever the Bookmarks file had been changed. Here is the code of my script:
Code:
 #!/bin/bash

        filetomonitor=$(echo "Bookmarks")

        function cpfile(){
                timelabel=$(stat $HOME/.config/google-chrome/Default/Bookmarks | grep Modify | awk '{print $2"_"$3}' | awk '{gsub(/\..+/,"");print}')
                cp -p $HOME/.config/google-chrome/Default/Bookmarks $HOME/Dropbox/Chrome_Bookmarks/${filetomonitor}_${timelabel}
        }

        while true; do
        change=$(inotifywait -e delete_self,moved_to,create $HOME/.config/google-chrome/Default/)
        change=${change#$HOME/.config/google-chrome/Default/ * }
        if [ "$change" = "Bookmarks" ]; then cpfile; fi
        done
I name my script as Chrome_Backup. So, if I do
Code:
#!/bin/bash

Chrome_Backup
/usr/bin/google-chrome
The inotifywait will start to monitor my Bookmarks file, but the google-chrome can not be invoked. I have tested it with
Code:
top -b -n 1 | grep chrome
no chrome process were found.


If I do
Code:
#!/bin/bash

/usr/bin/google-chrome
Chrome_Backup
the google-chrome browser can pop-up, but this time inotifywait can not be invoked.


I also tried add one line at bottom of /usr/bin/google-chrome to run Chrome_Backup. Then I run google-chrome from terminal, the chrome still work but inotifywait can not be invoked.

The code of my script should be fine, since it works if I run it and chrome separately.

I do not know why it dose not work if I run both in one script. Any one has any idea?

Last edited by cristalp; 10-15-2013 at 02:15 PM.
 
Old 10-16-2013, 04:51 AM   #4
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Following the same idea of my backup script, if I want to write another backup script for Latex. That is, whenever I finish my writing and turn-off my texmaker editor, the script automatically send a copy of the latest saved .tex, .aux, .bib files to my Dropbox directory. How can I achieve this?

I do not want to work directly in my Dropbox directory, because that will mess up my other things and I also want to make backup copies to several other cloud servers, not only Dropbox.

Last edited by cristalp; 10-16-2013 at 04:56 AM.
 
Old 10-17-2013, 04:40 PM   #5
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,818

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Quote:
Originally Posted by cristalp View Post
Thanks for your answer. I have tried it. However, it does not work for me. Actually, my script did the job to automatically backup my chrome bookmarks to my Dropbox directory whenever the Bookmarks file had been changed. Here is the code of my script:
Code:
 #!/bin/bash

        filetomonitor=$(echo "Bookmarks")

        function cpfile(){
                timelabel=$(stat $HOME/.config/google-chrome/Default/Bookmarks | grep Modify | awk '{print $2"_"$3}' | awk '{gsub(/\..+/,"");print}')
                cp -p $HOME/.config/google-chrome/Default/Bookmarks $HOME/Dropbox/Chrome_Bookmarks/${filetomonitor}_${timelabel}
        }

        while true; do
        change=$(inotifywait -e delete_self,moved_to,create $HOME/.config/google-chrome/Default/)
        change=${change#$HOME/.config/google-chrome/Default/ * }
        if [ "$change" = "Bookmarks" ]; then cpfile; fi
        done
I name my script as Chrome_Backup. So, if I do
Code:
#!/bin/bash

Chrome_Backup
/usr/bin/google-chrome
The inotifywait will start to monitor my Bookmarks file, but the google-chrome can not be invoked.
That's because you're calling both commands sequentially in the foreground. Chrome won't run until the backup exits.

I would swap the order, so chrome is run first, and stick a "&" on the end to fork it to the background. Then use "$!" to grab the PID of the chrome process, and pass that as an argument to Chrome_Backup. Inside Chrome_Backup you would monitor that PID, and exit when the PID is no longer running.

Alternatively, you could fork both processes to the background, then dive into an endless while loop that monitors the chrome PID. When the chrome PID exits, you could force kill the PID of the backup script (or however else you normally exit the backup script).

Something like:
Code:
#!/bin/bash

/usr/bin/google-chrome &
chrome_pid=$!
Chrome_Backup &
backup_pid=$!

while true; do
   ps -p $chrome_pid >/dev/null
   if [[ $? -eq 1 ]]; then
      kill $backup_pid
      exit 0
   fi
   sleep 1
done

Last edited by suicidaleggroll; 10-17-2013 at 04:47 PM.
 
Old 10-17-2013, 10:17 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
Couldn't you just

Code:
#!/bin/bash

Chrome_Backup &
backup_pid=$!

/usr/bin/google-chrome

kill $backup_pid
exit 0
 
Old 10-17-2013, 10:52 PM   #7
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,818

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Yep that would probably work too, and is a bit more graceful than my last reply. I'm not sure how the backup script needs to be killed though, maybe it needs to be "politely" stopped?

Last edited by suicidaleggroll; 10-17-2013 at 10:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Software to synchronize two folders at one disc jured Linux - Software 2 02-19-2011 04:09 AM
Synchronize two directories (bash script) SydPersia Programming 2 02-11-2011 05:20 PM
need software to synchronize files automatically asifbasha Linux - Software 1 05-02-2009 10:12 AM
Need a script to synchronize with public time servers basis1980 Linux - Newbie 3 11-28-2007 09:34 AM
Software to synchronize your files between two systems? matthias_k Linux - Software 1 11-03-2006 09:22 AM


All times are GMT -5. The time now is 01:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration