Search code examples
cbashubuntusynchronizationrsync

How to invoke and terminate all background looping scripts from C program in Ubuntu?


I am fairly new to Ubuntu, so I apologize for any bad questions.

I am making a C program that syncs multiple folders to remote destinations. I am planning to invoke this script from the C program: Automatic synchronization via rsync.

The script only works for 1 directory, so I will have to have many instances to cover each directory. Therefore, I want to run the scripts in the background instead of terminals popping up everywhere.

Then there's also the problem of how do I kill all the background scripts that are endlessly looping when user decides to stop synchronization.

Thanks in advance!


Solution

  • You could try to use pkill or killall first :

    $ pkill <script name>
    # or ...
    $ killall <script name>
    

    If you want to force the kill of your process list :

    $ ps -ef|awk '($0 ~ "<processname>"){print $2}'|while read -r; do kill -9 $REPLY; done
    

    The column number of the PID (2 in my example) may change, depending on the OS.