SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question 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:
    Code:
    sudo ntpdate ntp.ubuntu.com pool.ntp.org
    2. Automatically enter the system password:
    Code:
    helloworld

    Thanks for any help.

  2. #2
    SitePoint Addict
    Join Date
    Aug 2013
    Location
    New Zealand
    Posts
    277
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    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:
    Code:
    sudo ntpdate ntp.ubuntu.com pool.ntp.org
    2. Automatically enter the system password:
    Code:
    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.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,627
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •