So the problem stems from having a slow internet connection, and I have problems (debian) when I try to run 'apt-get update'. Apt-get pulls down 4 small pieces of data and tries to pull down 1 large piece, after about 5 minutes and 25% complete it gives up on the large download and instead tries to get an even larger alternative. 5 minutes after that it gives up at 20% done and instead tries another even lager alternative, and 5 minutes later and 10% done it gives up and complains it could not get what it needs. If I try running 'apt-get update' again it pulls down the 4 small pieces again and sequentially continues the 3 large pieces from where it left each of them. I have tried researching and playing with apt's settings for http::Timeout and ftp::Timeout, but it seems like there isn't a setting to deal with this situation. My current practice is to babysit this and ctrl-c when apt moves on to an alternative, to avoid the extra wasted 10 minutes per attempt. I want to be able to get it to babysit itself.

What I have in mind is a shell script along the lines of:

start a timeout, sudo apt-get update, if at any time apt-get completes successfully then immediately go on to run sudo apt full-upgrade and eventually exit, but in the meantime if apt-get prints a line to screen beginning with the text "Ign:" then pkill apt, and if less than 30 minutes have expired then start over from sudo apt-get update, else echo "Apt-update could not complete at this time!" and exit.

What I would like to know is, can a bash script actually do such stuff? If yes then a tip on what command to start looking up would be helpful. I think the checking and the running of apt-get would have to be in 2 processes but I don't know how one would check the screen output and exit status of the other. If a bash script can't do it then I have no idea what other tool I should look at, can Python do it?

  • 1
    XY problem! Fix the network problem first, rather than kludging in corrective hacks.
    – waltinator
    Sep 13, 2020 at 17:33

1 Answer 1


You have a complex question here.

  1. Can a script monitor what a program prints to the screen?

Yes, strace can do this.

strace -p<process id> -s1000 -e write

This will watch what the process belonging to process id is writing, increasing the output to 1000 chars from default 32.

  1. Can you do things when a command outputs some string?

This is pretty simple and should get you going in the right direction.

<command-you-want-to-watch> | tee file_to_watch

tee will output to the screen as well as to a file. Although, you might not get some of the fancy overwriting on the screen that normally happens. If you don't care about watching, you can just redirect output to the file <command> >file.

watcher script:


while true; do
  if grep -q "phrase1" file_to_watch; then
    #do stuff

  if grep -q "phrase2" file_to_watch; then
    #do stuff

  sleep 1

This will check the file once per second for a phrase, do whatever you like and then end (the break statements are what kill the infinite loop)

You must log in to answer this question.

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