Run Scripts in Background

 

  1. remove the ampersand, enter your password and then pause the task with Ctrl+Z and then run bg so the job resumes running in the background ;
  2. Dirty workaround: do a dummy sudo before (like sudo ls), then launch your nohupcommand, and it won’t ask for your password again and will run anyway.
Workaround with sudo with nohup

$ sudo date
# Date executes and the password is cached for a few minutes.
$ nohup sudo some_script &
# Executes without asking for a password, since it's cached.

 

Commands can produce output on stdout or stderr. The commands that you tried redirected only stdout. Under bash, you can redirect the output from both streams at once using:

command &>test.txt &

Or

command &>/dev/null &

If you are using a POSIX shell, then you need to do the redirection in steps:

command >test.txt 2>&1 &

>test.txt redirects stdout to the file test.txt. Because stderr is file handle 2, the effect of 2>&1 is to redirect stderr to wherever stdout, denoted by &1 is currently going.

nohup ./my-shell-script.sh >test.log 2>&1 &

1. Execute a command in the background using &

You can execute a command (or shell script) as a background job by appending an ampersand to the command as shown below.

$ ./my-shell-script.sh &

2. Execute a command in the background using nohup

After you execute a command (or shell script) in the background using &, if you logout from the session, the command will get killed. To avoid that, you should use nohup as shown below.

$ nohup ./my-shell-script.sh &

3. Execute a command using screen command

After you execute a command in the background using nohup and &, the command will get executed even after you logout. But, you cannot connect to the same session again to see exactly what is happening on the screen. To do that, you should use screen command.

Linux screen command offers the ability to detach a session that is running some process, and then attach it at a later time. When you reattach the session later, your terminals will be there exactly in the way you left them earlier.

4. Executing a command as a batch job using at

Using at command you can schedule a job to run at a particular date and time. For example, to execute the backup script at 10 a.m tomorrow, do the following.

$ at -f backup.sh 10 am tomorrow

5. Execute a command continuously using watch

To execute a command continuously at a certain interval, use watch command as shown below.

watch df -h

Sendmail Milter

https://metacpan.org/pod/Sendmail::Milter

Sendmail::Milter – Interface to sendmail’s Mail Filter API

Sendmail::Milter is a Perl extension to sendmail’s Mail Filter API (Milter).

With this module, Perl callbacks can be defined and registered with the Milter engine. This module calls those perl callbacks using interpreters from a threaded persistent interpreter pool.

use Sendmail::Milter;

  my %my_milter_callbacks =
  (
    'connect' => \&my_connect_callback,
    'helo' =>    \&my_helo_callback,
    'envfrom' => \&my_envfrom_callback,
    'envrcpt' => \&my_envrcpt_callback,
    'header' =>  \&my_header_callback,
    'eoh' => \&my_eoh_callback,
    'body' =>    \&my_body_callback,
    'eom' => \&my_eom_callback,
    'abort' =>   \&my_abort_callback,
    'close' =>   \&my_close_callback,
  );

  sub my_connect_callback;
  sub my_helo_callback;
  sub my_envfrom_callback;
  sub my_envrcpt_callback;
  sub my_header_callback;
  sub my_eoh_callback;
  sub my_body_callback;
  sub my_eom_callback;
  sub my_abort_callback;
  sub my_close_callback;


  BEGIN:
  {
    # Get myfilter's connection information
    # from /etc/mail/sendmail.cf

    Sendmail::Milter::auto_setconn("myfilter");
    Sendmail::Milter::register("myfilter",
        \%my_milter_callbacks, SMFI_CURR_ACTS);

    Sendmail::Milter::main();

    # Never reaches here, callbacks are called from Milter.
  }
Registers a mail filter NAME with hash reference CALLBACKS callbacks, and
optional capability flags FLAGS. NAME is the same filter name that you would
pass to B. CALLBACKS is a hash reference that can contain any of
the following keys:

  connect
  helo
  envfrom
  envrcpt
  header
  eoh
  body
  eom
  abort
  close

The values for these keys indicate the callback routine that is associated with
each Milter callback. The values must be either function names, code references
or closures.

This function returns nonzero upon success, the undefined value otherwise.
Each Milter callback could quite possibly run in a
different instance of the Perl interpreter.

=item B<eom_callback> CTX

Invoked at end of message. This routine can perform special operations such as
modifying the message header, body, or envelope. See the section on
B<eom_callback> in B<Milter Context Functions>.