• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

crontab

Bash Script on Startup

December 23, 2021

How to add bash scripts on startup.

update-rc.d

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh
sudo update-rc.d /etc/init.d/yourscript.sh defaults
chmod +x /etc/init.d/yourscript.sh

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh sudo update-rc.d /etc/init.d/yourscript.sh defaults chmod +x /etc/init.d/yourscript.sh

Root Crontab

sudo crontab -e
@reboot /path/to/yourscript.sh

sudo crontab -e @reboot /path/to/yourscript.sh

Filed Under: Linux Tagged With: bash, crontab, script, startup, update-rc.d

Display Instance ID & IP Address

July 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

June 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

June 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

January 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

December 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

  • Home
  • About
  • Archives

Copyright © 2023