1

I need to 'repo sync' the Android source overnight, as my monthly bandwidth is limited.

, I start the 'repo sync' at midnight, then want to kill it at 8am. The following is the command:

2 0 * * * /home/user/android-src/reposync.sh

reposync.sh contains:

cd /home/user/android-src
/home/user/bin/repo sync

I was hoping to kill it with the following cron entries:

55 7 * * * ps -eo pid,args | grep repo | awk '{print $1}' | xargs -I {} kill {}
0 8 * * * ps -eo pid,args | grep repo | awk '{print $1}' | xargs -I {} kill -9 {}

The problem is that 'repo sync' spawns many processes - lots of 'git', 'curl', 'git-remote-http', 'python', (and I think a 'bash' and an 'sh', as I put the repo sync command into a .sh script). (Apologies for my ignorance/terminology.)

How can I find the processes to kill them? Or is there a better way? Will repo run as a normal user and I can just kill all that user's tasks? I'd like to leave my computer running over a few days unattended, and have it connect up every night and disconnect every morning, although any solutions gratefully considered, including the ugly, e.g., should I kill the internet connection so repo times out then resume it? There is nothing else running on the server, (VirtualBox on Win7 x64 running Ubuntu 10.04), so should I just reboot every 8am?

2 Answers 2

1

Use a negative argument to kill to kill the entire process group.

#!/bin/sh
ps -eo pid,args |
awk '/[r]epo/{print $1}' |
xargs -I {} kill "$@" -{}

Note also the ancient FAQ about how to avoid the useless grep | grep -v grep | awk.

Invoke this like

55 7 * * * /path/to/killsync
0 8 * * * /path/to/killsync -9

We hope the second one will never actually be needed; that's hopefully useless too.

See https://stackoverflow.com/questions/11000538/linux-kill-with-negative-pid

10
  • I'm getting ERROR: unknown signal name "13738". It is the correct PID. Unfortunately I'm out of my depth trying to figure out what the kill parameter "$@" is or does.
    – Chris
    Jun 3, 2013 at 23:46
  • In a shell script, "$@" expands to a properly quoted list of the script's arguments. So if you say killsync -s 9 then "$@" will be -s 9 inside the script.
    – tripleee
    Jun 4, 2013 at 4:29
  • Does the kill documentation on your system mention the negative PID convention? Maybe as a workaround, always pass in an explicit signal number, i.e. run the first script as killsync -15.
    – tripleee
    Jun 4, 2013 at 4:30
  • You're right triplee thanks, my kill docs don't mention negative PID (updated Ubuntu 12.04). Will sending -15 kill the rest of the process group too?
    – Chris
    Jun 9, 2013 at 13:35
  • 1
    Oops very sorry, done.
    – Chris
    Jun 14, 2013 at 5:56
0

The process you need to kill has spawned child processes that need to be killed also:

vi killemall.sh
---------------
#!/bin/bash
# 
# Get the process PID
# Kill all the processes that have a parent pid = PID
# Kill the master process
#
MASTERPID=`ps -eo pid,args | grep repo | awk '{print $1}'`
ps -ef | grep ${MASTERPID} | awk '{print $1}'` | while read PIDX; do kill -9 ${PIDX} 2>/dev/null; done;

Will repo run as a normal user and I can just kill all that user's tasks?

# Should work, but it's a complete overkill

should I kill the internet connection so repo times out then resume it? so should I just reboot every 8am?

# No need to come up with "excentric" solutions. Follow the Kill-Em-ALL approach, and that should solve your issue.
4
  • This doesn't seem to work, perhaps because the MASTERPID=ps.. command returns two PIDs. One is the repo process, a python command, and I assume the other PID is that of the MASTERPID=ps.. command itself, as I can't find the PID when I look at ps aux, and it seems it'd catch its own arg. How can I exclude its own process, (if my diagnosis is correct)?
    – Chris
    May 31, 2013 at 14:45
  • I was wrong, the second process is grep --color=auto repo. So I tried: MASTERPID=``ps -eo pid,args | grep repo | grep -v color=auto | awk '{print $1}'``` ps -ef | grep ${MASTERPID} | awk '{print $1}'` | while read PIDX; do kill -9 ${PIDX} 2>/dev/null; done;` But nothing dies.
    – Chris
    May 31, 2013 at 15:07
  • Sorry for the punctuation - editing forbidden. I tried adding 'grep -v color=auto' which does then return only one PID, but it isn't killed.
    – Chris
    May 31, 2013 at 15:14
  • Just noticed the WHILE loop in your 2nd line. Great thing to learn, and means my above comments are utterly irrelevant.
    – Chris
    Jun 3, 2013 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .