LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Synchronize a script with another software in linux (https://www.linuxquestions.org/questions/programming-9/synchronize-a-script-with-another-software-in-linux-4175480879/)

cristalp 10-15-2013 10:44 AM

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.

TB0ne 10-15-2013 11:09 AM

Quote:

Originally Posted by cristalp (Post 5046179)
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.

cristalp 10-15-2013 02:12 PM

Quote:

Originally Posted by TB0ne (Post 5046203)
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?

cristalp 10-16-2013 04:51 AM

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.

suicidaleggroll 10-17-2013 04:40 PM

Quote:

Originally Posted by cristalp (Post 5046307)
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


ntubski 10-17-2013 10:17 PM

Couldn't you just

Code:

#!/bin/bash

Chrome_Backup &
backup_pid=$!

/usr/bin/google-chrome

kill $backup_pid
exit 0


suicidaleggroll 10-17-2013 10:52 PM

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?


All times are GMT -5. The time now is 04:09 AM.