I am trying to extract files from a tape device using tar and am getting IO errors:

tar: /dev/nst0: Cannot read: Input/output error

However, tar tries to continue anyway:

tar: Skipping to next header

Is there a way to tell tar to exit directly after the first occurrence of the Cannot read error? I worked my way through the man pages and the tar manual, but could not find anything like that. Or is there a way to catch the error message in the calling shell and kill tar from outside?

The OS is Ubuntu 16.04 with kernel 4.4.0-38 and tar is GNU tar 1.28.

  • Which OS and which tar implementation are you using? – user1686 Nov 26 '16 at 21:32
  • The OS is Ubuntu 16.04 with kernel 4.4.0-38 and tar is GNU tar 1.28. – allion Nov 26 '16 at 22:20

You could use an expect script:

#!/usr/bin/expect -f

# Timeout: 10000 seconds
set timeout 10000

spawn tar your tar args come here
# spawn custom-tar-script.sh

expect "tar: /dev/nst0: Cannot read: Input/output error"

send_user "Will kill PID <[exp_pid]>.\n"
exec kill [exp_pid]
# exec killall tar
# exec custom-kill-script.sh

(Adapt it to your needs)

  • Thanks, looks like this does the "killing from outside" that I had in mind. When the timeout is over does it terminate the spawned script? – allion Nov 29 '16 at 17:14
  • Yes, for that reason I increased the timeout (~10 seconds is default). – u_Ltd. Nov 29 '16 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.