• Skip to primary navigation
  • Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

crontab

Display Instance ID & IP Address

by Ulysses · Jul 7, 2019

How to display IP address and Instance ID on web page behind a AWS load balancer.

Add these 2 commands in crontab. The job runs every 5 mins.

*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/instance-id > /var/www/html/id.txt
*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4 > /var/www/html/ip.txt

*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/instance-id > /var/www/html/id.txt */5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4 > /var/www/html/ip.txt

You can view on the site.

# your domain
http://yourdomain.com/ip.txt
http://yourdomain.com/id.txt
# your server ip address
http://1.1.1.1/ip.txt
http://1.1.1.1/id.txt

# your domain http://yourdomain.com/ip.txt http://yourdomain.com/id.txt # your server ip address http://1.1.1.1/ip.txt http://1.1.1.1/id.txt

Filed Under: Cloud Tagged With: aws, crontab, ec2, instance-id, ip address, metadata

Crontab Editor

by Ulysses · Jun 16, 2019

If you want to change the crontab editor, run the following:

$ select-editor

$ select-editor

Choose one.

Filed Under: Linux Tagged With: change, crontab, editor

Crontab File

by Ulysses · Jun 12, 2019

How to load crontab from file.

crontab /path/to/file.txt

crontab /path/to/file.txt

Save crontab to a file.

crontab -l > /path/to/file.txt

crontab -l > /path/to/file.txt

Filed Under: Linux Tagged With: crontab, file, load, write

Adding to Crontab in Bash

by Ulysses · Jan 10, 2019

Here’s a neat command to append a job to a crontab within a Bash script.

crontab -l | { cat; echo "0 1 * * * /dir/script.sh > /logs/script.cronlog 2>&1"; } | crontab -

crontab -l | { cat; echo "0 1 * * * /dir/script.sh > /logs/script.cronlog 2>&1"; } | crontab -

To validate, perform crontab -l to see if your changes are there.

crontab -l

crontab -l

Filed Under: Linux Tagged With: bash, cron, crontab

Bash Source in Cron

by Ulysses · Dec 16, 2018

I’m running a “source” command inside a Bash script. The script is running fine on its own, but when I try running it inside a crontab, it doesn’t work. I’m using the source command just to load variables in a file, so I don’t have to edit multiple files. As it turns out, the regular shell in crontab does not quite understand the source command, and it’s the very reason the script fails. Here’s an example.

source /home/user/server.conf

source /home/user/server.conf

I ended up removing the source line, and hardcoding the variables instead of loading it from a file.

server="servername"

server="servername"

Filed Under: Linux Tagged With: bash, crontab, shell, source

Copyright © 2012–2021