Linux Ubuntu - Creating auto executing script

Hi

I come from a windows background and in windows there was .bat files that could run commands automatically. Now since I have swiched to linux (Ubuntu) i beilieve it does not support .bat files, so I was wondering can someone please help me create a script which when run, will do the following:

  1. Run Command:
sudo ntpdate ntp.ubuntu.com pool.ntp.org
  1. Automatically enter the system password:
helloworld

Thanks for any help.

Normally I suffix script files with .sh extension and run them with the “source” command something like the following:

$ chmod +x my-script.sh
$ source my-script.sh

Wheras in your case the script file will be something like the following:

#!/bin/bash
sudo ntpdate ntp.ubuntu.com pool.ntp.org < helloworld

#-- end of script

Not 100% sure that < will work for a password prompt.

Generally it is considered a bad idea to put passwords inside scripts. I guess it depends on how much your system would be compromised if the password was discovered by a hacker. I don’t know much about ntpdate but maybe add that command into the /etc/sudoers config file allowing you to run that command without a password.

For the ntp update you could just add the command to a cron job which by default runs at root. man crontab for details and will also avoid handling the password in general.