Creating auto executing script

Forum rules
Before you post please read this

Creating auto executing script

Postby cancer10 on Sun Dec 15, 2013 12:52 am

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 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

2. Automatically enter the system password: helloworld


Thanks for any help.
cancer10
Level 1
Level 1
 
Posts: 6
Joined: Wed Oct 30, 2013 12:14 am

Linux Mint is funded by ads and donations.
 

Re: Creating auto executing script

Postby xenopeek on Sun Dec 15, 2013 4:49 am

This isn't possible in the way that you suggest. Using sudo, you will have to type the password, unless you reconfigure sudo to not need a password for the ntpdate command for your account. That's a bit more difficult.

Could you let us know what you are trying to do here?

Each time your network interface goes up, ntpdate is run (see the file /etc/network/if-up.d/ntpdate) and uses ntp.ubuntu.com (see the file /etc/default/ntpdate). If you want it to also use pool.ntp.org (though you had better include a country code in that I think), you can just add that in /etc/default/ntpdate.

There are various ways to run system commands automatically without having to the enter a password. Clearly, you do that by running the scripts not from your account but from root's account. But before I write more about that, it would be helpful to understand what you are trying to do :)
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13679
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Creating auto executing script

Postby Habitual on Mon Dec 16, 2013 9:52 am

My DorkBlog
Cirrhus9.com - Managed HA VDSs and Scalable grid solutions.
User avatar
Habitual
Level 7
Level 7
 
Posts: 1926
Joined: Sun Nov 21, 2010 8:31 pm
Location: Under the hood

Re: Creating auto executing script

Postby cancer10 on Wed Dec 18, 2013 6:14 am

xenopeek wrote:This isn't possible in the way that you suggest. Using sudo, you will have to type the password, unless you reconfigure sudo to not need a password for the ntpdate command for your account. That's a bit more difficult.

Could you let us know what you are trying to do here?

Each time your network interface goes up, ntpdate is run (see the file /etc/network/if-up.d/ntpdate) and uses ntp.ubuntu.com (see the file /etc/default/ntpdate). If you want it to also use pool.ntp.org (though you had better include a country code in that I think), you can just add that in /etc/default/ntpdate.

There are various ways to run system commands automatically without having to the enter a password. Clearly, you do that by running the scripts not from your account but from root's account. But before I write more about that, it would be helpful to understand what you are trying to do :)


Thanks for your reply.

To give you a summary of why I need this script is because a couple of days ago I posted a question on this forum, but the solution I got does not work properly.

So I was thinking to write a script that I can run once I connect to the internet so that my system time is updated.

If you can give me a solution, i'd be greateful to you.


Thanks
cancer10
Level 1
Level 1
 
Posts: 6
Joined: Wed Oct 30, 2013 12:14 am

Re: Creating auto executing script

Postby xenopeek on Wed Dec 18, 2013 1:20 pm

The easiest way to do this is by editing the file /etc/rc.local, as also shown on your linked topic. To edit that file open a terminal or press Alt+F2 for the run dialog and give the command:
Code: Select all
gksudo gedit /etc/rc.local

Just add the line you want to execute, without the sudo prefix, before the final "exit 0" line in the file /etc/rc.local. So insert the line:
Code: Select all
ntpdate ntp.ubuntu.com pool.ntp.org

ntpdate is not ntpd BTW, but I take it you understand that. ntpd isn't installed on Linux Mint by default IIRC, but indeed ntpdate is.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13679
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests