-1

I want to make a script that will prevent requests to certain domains for a certain amount of time, and kill specific processes during the same amount of time.

I would like to have something like a daemon, to which I could send commands. For example, to see how much time is left, with some_script timeleft, to start the daemon which would be created by something like some_script start, or to add a new domain/process, etc.

What I'm stuck on is :

  • How to create a daemon? I have seen this

  • I don't know how to send a command to a daemon from a command line

I hope I have been clear enough in my explanation.

Community
  • 1
  • 1
cocosushi
  • 144
  • 10
  • 1
    Why don't you make it HTTP-based so you don't have to invent an entirely new server (which is far outside your skill set right now)? – ikegami Dec 19 '16 at 21:06
  • 1
    @cocosushiTS: You could write your own server from scratch, or you could use existing modules to create an HTTP server – Borodin Dec 19 '16 at 21:15
  • 1
    @cocosushiTS: You may create your own server using any inter-process communication that you choose. Sockets is one way, but they work at a very low level and you will have to design a simple protocol. Since there is so much ready-made software to implement an HTTP server, it makes sense to write something that will respond to an HTTP request, which is mostly plain text – Borodin Dec 19 '16 at 21:28
  • 1
    @cocodushiTS: There is no standard way to send commands to a daemon process. You have to write that process so that it waits for commands using your choice of inter-process communication, and write your "simple script" to send commands in the form that your daemon expects. – Borodin Dec 19 '16 at 21:36
  • 2
    @cocosushi TS, Overkill? No. Writing everything from scratch because you think it's going to be more efficient is what's overkill. Get the application running first, and if you still think there's a reason to integrate the server logic then, do it then. – ikegami Dec 19 '16 at 21:37
  • 1
    You could use the technique in the example you link to in order to send messages to your daemon - maybe send it a SIGHUP (with `kill -HUP `) to make it re-read the file of sites it has to block and send SIGUSR1 to make it print how long it has to run etc.... – Mark Setchell Dec 19 '16 at 22:59

2 Answers2

1

I would probably use the bones of the answer you refer to, but add:

  • a handler for SIGHUP which re-reads the config file of IPs to suppress, and,

  • a handler for SIGUSR1 which reports how much time there is remaining.

So, it would look like this roughly:

#!/usr/bin/perl

use strict;
use warnings;
use Proc::Daemon;

Proc::Daemon::Init;

my $continue = 1;
################################################################################
# Exit on SIGTERM
################################################################################
$SIG{TERM} = sub { $continue = 0 };

################################################################################
# Re-read config file on SIGHUP
################################################################################
$SIG{HUP} = sub {
   # Re-read some config file - probably using same sub that we used at startup
   open(my $fh, '>', '/tmp/status.txt');
   print $fh "Re-read config file\n";
   close $fh;
};

################################################################################
# Report remaining time on SIGUSR1
################################################################################
$SIG{USR1} = sub {
   # Subtract something from something and report difference
   open(my $fh, '>', '/tmp/status.txt');
   print $fh "Time remaining = 42\n";
   close $fh;
};

################################################################################
# Main loop
################################################################################
while ($continue) {
     sleep 1;
}

You would then send the HUP signal or USR1 signal with:

pkill -HUP daemon.pl

or

pkill -USR1 daemon.pl 

and look in /tmp/status.txt for the output from the daemon. The above commands assume you stored the Perl script as daemon.pl - adjust if you used a different name.

Or you could have the daemon write its own pid in a file on startup and use the -F option to pkill.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
-2

There are a few ways to communicate with a daemon, but I would think UNIX domain sockets would make the most sense. In perl, IO::Socket::UNIX would be a thing to look at.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52