9 Replies - 1477 Views - Last Post: 19 July 2007 - 10:13 AM Rate Topic: -----

#1 Captain M  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Simple program to edit a file

Posted 11 July 2007 - 01:14 PM

I wrote a simple program to edit a text file on my computer. I had a problem that every time I started up my computer, I would have to manually change the order of my dns servers. It got really annoying, and I didn't know how to fix it in the os, so I just wrote a simple little program to do it for me. The problem is, the file is only writeable by root, causing me to have to type my password in. This causes the computer to not be able to run the program by itself. How can I make the program work? Here is my code for referance:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main (){
	system("sudo cp /home/family/resolv.conf /etc");
	 return 0;
}


And here is the file it copies:
# generated by NetworkManager, do not edit!

search domain.actdsltmp


nameserver 205.171.3.65
nameserver 192.168.0.1


Is This A Good Question/Topic? 0
  • +

Replies To: Simple program to edit a file

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5327
  • View blog
  • Posts: 27,246
  • Joined: 10-May 07

Re: Simple program to edit a file

Posted 13 July 2007 - 08:21 AM

I thought that resolv.conf existed in /etc/?

A workaround you could make a cron job to run your script.

Check your sudo settings to remove the password option.

sudo gedit /etc/sudoers



The file is probably empty. If so, add this line :
username ALL=(ALL) ALL



I'm not familiar with sudo, so if this information is incorrect, somene post the correct solution.

This post has been edited by no2pencil: 13 July 2007 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Simple program to edit a file

Posted 13 July 2007 - 09:05 AM

A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5327
  • View blog
  • Posts: 27,246
  • Joined: 10-May 07

Re: Simple program to edit a file

Posted 13 July 2007 - 09:27 AM

View PostMorphiusFaydal, on 13 Jul, 2007 - 09:05 AM, said:

A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.

You mean like put it in /etc/rc.d/rc.local ? That runs after gpm! Or under one of the rc.S* run levels, yeah. I can see that working, good idea.
Was This Post Helpful? 0
  • +
  • -

#5 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Simple program to edit a file

Posted 13 July 2007 - 11:20 AM

View Postno2pencil, on 13 Jul, 2007 - 09:27 AM, said:

View PostMorphiusFaydal, on 13 Jul, 2007 - 09:05 AM, said:

A better way to do it would be to set it up in a bash shell script, and add it to the default runlevel, so it runs on every boot.

You mean like put it in /etc/rc.d/rc.local ? That runs after gpm! Or under one of the rc.S* run levels, yeah. I can see that working, good idea.


I think the bigger problem is why does he need to rewrite /etc/resolv.conf on every boot? Why not set dhcpcd or dhclient to not overwrite it everytime it runs?
Was This Post Helpful? 0
  • +
  • -

#6 Captain M  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Simple program to edit a file

Posted 16 July 2007 - 08:53 AM

Sorry, I've been gone. I have no clue why I have to edit the file every time. I'm going to answer your questions in the order they were asked. no2pencil, resolv.conf does exist in /etc, I just have another one sitting in my home folder that copies over the one in /etc. If you notice, the last thing my code says is /etc, as that tells it where to copy to. You probably already knew this and just missed the last part of my code. And as for no2pencil's solution, would that not just make it so you never have to type a password to sudo? That's dangerous and something I don't want. I'm pretty sure somewhere in this ubuntu system ages ago I found list of programs that run on startup, and just added mine to it. I can't seem to find it now though. I don't really understand what you're saying about editing that other file, could you explain it more?
Was This Post Helpful? 0
  • +
  • -

#7 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Simple program to edit a file

Posted 16 July 2007 - 12:49 PM

View PostCaptain M, on 16 Jul, 2007 - 08:53 AM, said:

Sorry, I've been gone. I have no clue why I have to edit the file every time. I'm going to answer your questions in the order they were asked. no2pencil, resolv.conf does exist in /etc, I just have another one sitting in my home folder that copies over the one in /etc. If you notice, the last thing my code says is /etc, as that tells it where to copy to. You probably already knew this and just missed the last part of my code. And as for no2pencil's solution, would that not just make it so you never have to type a password to sudo? That's dangerous and something I don't want. I'm pretty sure somewhere in this ubuntu system ages ago I found list of programs that run on startup, and just added mine to it. I can't seem to find it now though. I don't really understand what you're saying about editing that other file, could you explain it more?


When your system boots, it runs a collection of scripts to get everything that is needed to run your computer, running. There is one specific script that you can add your own stuff to. So, you would create a small bash shell script like such:

#!/bin/bash
cp /root/resolv.conf /etc/resolv.conf
echo "resolv.conf copied..."



And save as /root/myResolv.sh or something like that. chmod +x myResolv.sh to make it executable, and add /root/myResolv.sh to local.start. (local.start is the Gentoo-version of the custom init scripts... I don't know where Ubuntu keeps theirs.

Although, if I recall correctly, and someone check me on this, you would actually name it something like "77-myResolv" and put it in /etc/rc.d/rc.3, still make it executable, and then it'll run on it's own.

Of course, in that script, you'll have to modify where that resolv.conf is. I'd recommend putting it in /root, just so if you screw something up as user, and your ~ gets blown away, you don't get screwed next time you boot.
Was This Post Helpful? 0
  • +
  • -

#8 Captain M  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Simple program to edit a file

Posted 17 July 2007 - 06:58 PM

Hmm I don't know. That sort of sounds risky. Is it?
Was This Post Helpful? 0
  • +
  • -

#9 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Simple program to edit a file

Posted 17 July 2007 - 10:09 PM

View PostCaptain M, on 17 Jul, 2007 - 06:58 PM, said:

Hmm I don't know. That sort of sounds risky. Is it?


It's doing exactly the same thing that the program you want to write is doing.

Either way, you're calling something at boot time to copy one file from one place to another. The program you want to write, if you want it to run automagically, will have to be called from a script in /etc/rc.d/rc.* anyway. So why bother writing a program to call 'cp' that's getting called from a script that can call cp itself? Cut out the middleman.

Scripting is all about the pursuit of laziness; getting the most done with as little work as possible.

More on the riskiness: That's why the rc-scripts are where they are. So you can add your own. Check the Ubuntu-docs for direction on exactly where to put it. #ubuntu on irc.freenode.net would also be a great place to ask if they can help you. No guarantees on the snideness of any given response, though.
Was This Post Helpful? 0
  • +
  • -

#10 Captain M  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Simple program to edit a file

Posted 19 July 2007 - 10:13 AM

Ok, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1