Skip to content

Tag: shell

bash and wget to download a lot of files

Problem:

  • Download 500 large files.
  • Files follow the pattern data_1.zip, data_2.zip, … data_500.zip.
  • All the files are in “https://examples.com/downloads”.
  • Files have from kilobytes to gigabytes in size.

Solution:

  • A naive Bash script to download the files using wget.
  • It is set to use wget with -c (continue). This will skip files already downloaded and continue files that have been interrupted. This only works with FTP/HTTP servers that support the “Range” header.
#!/bin/bash
set -euxo pipefail

PREFIX="https://examples.com/downloads/data_"

for i in $(seq 1 500); do
  wget -c "$PREFIX"_"$i".zip
done

if host pings execute command

[bash]#!/bin/sh
HOST="silveiraneto.net"
if ping -c 1 $HOST > /dev/null
then
echo your command
fi
[/bash]

  • replace silveiraneto.net for your desired hostname or ip
  • ping sends only one package (-c 1) and ignores the text output
  • a ping is successful returns 0 and the if proceeds
  • replace the echo line with your command

Congelando e Ressuscitando Processos

batman mr freeze

Nem só de morte vive o kill.

Suponha que você tem um processo chamado program e quer congelar seu funcionamento. Para congela-lo sem mata-lo você pode mandar um sinal SIGSTOP com:

kill -s stop `pidof program`

Para ressuscitar o mesmo processo:

kill -s cont `pidof program`