LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2024, 02:46 AM   #1
Jason.nix
Member
 
Registered: Feb 2023
Posts: 561

Rep: Reputation: 10
Post A script to add and remove a character in a line at a given time


Hello,
I have a configuration file and I want a line from this configuration file to be converted to a comment (#) at a specified time, and then reset the service, and again at a specified time, that line is removed from comment mode and reset the service again. Is this possible with a script?

Thank you.
 
Old 01-31-2024, 03:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes
you need to create a crontab entry to run your script which will do that modification and reset/restart/reload the service again.
To change a line you can use a lot of tools, for example awk, sed, perl, python or even shell. Or you can just store the two versions and copy them at the given time.
 
2 members found this post helpful.
Old 01-31-2024, 03:57 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by pan64 View Post
Or you can just store the two versions and copy them at the given time.
... or flip the target of a symlink between them.
 
Old 01-31-2024, 05:05 AM   #4
Jason.nix
Member
 
Registered: Feb 2023
Posts: 561

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by pan64 View Post
yes
you need to create a crontab entry to run your script which will do that modification and reset/restart/reload the service again.
To change a line you can use a lot of tools, for example awk, sed, perl, python or even shell. Or you can just store the two versions and copy them at the given time.
Hello,
Thank you so much for your reply.
Can you help me write this script? I want a # sign to be placed at the beginning of line 214 of the /etc/tor/torrc file at 12:00 AM and then the systemctl restart tor command will be executed, then tomorrow at the same time the # sign will be removed and the service will be restarted.
 
Old 01-31-2024, 06:12 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
As mentioned in #3 above, you can make two extra copies of the torrc file. Then at the designated times you can have some scripts do two things: First copy one of the copies to the official torrc file. Second restart the service / reload the configuration file.

You can do that with two shell scripts (one for each file) and trigger it via a cron job.

If you are amenable to that approach, please give it a try.
 
Old 01-31-2024, 10:51 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
One time actions are done with at jobs.
Next possible 12:00, in line 214 put a leading #
Code:
at 12:00 << _EOT_
PATH=/bin:/usr/bin:/sbin:/usr/sbin
sed -i '214 s/^/#/' /etc/tor/torrc
systemctl restart tor
_EOT_
One day later, in line 214 remove a leading #

Code:
at 12:00 + 1 day << _EOT_
PATH=/bin:/usr/bin:/sbin:/usr/sbin
sed -i '214 s/^#//'/etc/tor/torrc
systemctl restart tor
_EOT_
 
Old 01-31-2024, 10:59 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Code:
sed -i '214 s/^\([^#]\)/#\1/' /etc/tor/torrc
just to be on the safe side
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert character in positions then remove all spaces after inserted character substancev Linux - Newbie 11 05-06-2016 09:06 AM
i tried using this code for deleting a user given character from a user given string mecrazyme1234 Linux - Newbie 2 06-04-2011 04:59 PM
i tried using this code for deleting a user given character from a user given string mecrazyme1234 Programming 7 06-04-2011 11:47 AM
merge files, given its odd and even given timepassman Linux - Software 1 05-08-2008 01:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration