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

StackOverflow https://stackoverflow.com/questions/21214593

سؤال

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!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

I think your question is "how do you kill processes from C?" If so, the answer is to use the kill() function. You can send any signal, but SIG_KILL is the default used by the command line version of kill.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top