How can I cancel a copy process

I am initating a copy process from an applescript and would like to be able to cancel it from a seperate script if necessary.

this is how I am starting the copy

Does anyone know a shell script I can run to cancel it?

Thanks!
Dave

When you initiate a process such as “cp”, there is a PID (proces ID) associated with it. If you knew that PID then you could cancel/kill the process.

This script will get the PID of the cp process and kill it.

try
	set pid_line to do shell script "ps aux | grep cp | grep -v grep"
on error
	display dialog "Couldn't find the \"cp\" process" buttons {"OK"} default button 1
	return
end try

set pid to second word of pid_line
do shell script "kill " & pid

Note that this may mess up your script because your script is waiting for the shell to finish the cp process. A way to aviod that problem is to not have applescript wait for the cp process to finish. You can do that by adding “> /dev/null 2>&1 &” to the end of your command. This redirects the output of your command to /dev/null so that applescript doesn’t wait for the cp process to finish. You code then would look like…

do shell script "cp -R " & dvd_path & " /Users/admin/Desktop/DVD_rip/" & dvd_name & "/ > /dev/null 2>&1 &"
  • You don’t need the information from the -u option.

  • For this script, you certainly don’t want to kill another user’s process, so you don’t need the -a option.

  • I would add the -c option to list only the command name. (This should remove the need for the last grep.)

  • With the -c option added, I’d make the grep pattern more specific (i.e. match the end of the line). (Especially with something as short as “cp”, which could easily match “cpp”, “tcpdump”, and who knows what else.)

Alternatively:

do shell script "/bin/ps xc | /usr/bin/grep 'Safari$' | /usr/bin/grep --only-matching '^[[:space:]]*[0-9]\\+' | /usr/bin/sed -e 's/ //g'"

Hi Bruce,

for this purpose I’d prefer awk

do shell script "/bin/ps xc | /usr/bin/awk '/Safari$/ {print$1}'"

I’m not sure why I never picked up on awk… Thanks for sharing. :cool:

Both of those commands confuse me! But that’s why we have this forum… everyone can contribute. Thanks for cleaning up my shell command. My initial thought was to put “> /dev/null 2>&1 & echo $!” at the end of his command so he got the pid directly, but he said he wanted to do it in a separate script so my shell command was the best I could think of.

Code below logs to file, but i don’t want to wait forever. How i can have this code to log to file AND immediately continue my script. I can’t figure out how to add dev null to it.

do shell script "sudo tcpdump -i " & "ppp0" & " -vp" & " >> " & log_path user name "user" password "pin" with administrator privileges

Hello

Try adding:

 2>/dev/null &

to the right of your do shell script command. :wink:

you may also try to add and “ignoring application responses” block around the do shell script, if the above should fail.

as the name implies, “application responses” affects only applications, technically processes which receive and respond to Apple Events. AppleScript doesn’t send Apple Events to do shell script

Also don’t use sudo in do shell script commands, with administrator privileges is enough

:slight_smile:

I am sorry for misleading, I had forgotten about do shell script not receiving apple events.

But, if you assign something from a do shell script, to a variable, then events are coming back to you, not that those events can be ignored, I’m just adding to the picture.

Thanks. I tried to put it everywhere, but it always fails. What is the exact place it should be?

do shell script "sudo tcpdump -i " & "ppp0" & " -vp" & " >> " & log_path user name "user" password "pin" with administrator privileges

Normally commands from (Standard) Scripting Additions neither send nor receive Apple Events.