CRON and SYSLOG

            -- do not even try to persuade me, I have changed..
            -- but the world has not changed Orm.
               It is ruled by those who got the Stones.
               
            The Snow Queen: Magic Of The Ice Mirror, 2014, 17:59
# crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.

    1. nano (the simplest)
    2. mcedit
    3. ed
    4. vi

  Choose 1-4 []:

 ~

PROBLEM

  1. Allow VT8 (virtual terminal 8) on "autoPig" virtual computer.

  2. Show cron messages to VT8

  3. Cron must show message "Hello from $COUNTER" every minute, where COUNTER shows how many times crontab was applied.

  4. New /var/log/syslog messages should be doubled immediately to VT8

 ~

SOLUTION

  1. Edit console configuration:

    # nano /etc/default/console-setup
    

    file console-setup

    # CONFIGURATION FILE FOR SETUPCON
    
    # Consult the console-setup(5) manual page.
    
    ACTIVE_CONSOLES="/dev/tty[1-8]"
    
    CHARMAP="UTF-8"
    
    CODESET="Lat15"
    FONTFACE="TerminusBold"
    FONTSIZE="10x20"
    SCREEN_WIDTH="80"
    
    VIDEOMODE=
    
  2. # reboot

  3. Create file to store persistent data

    # touch /root/count
    

Edit crontab

# crontab -e
* * * * * sh -c /root/counter > /dev/tty8

Create file /root/counter

file counter

#!/bin/bash

for x in $(cat count) ; do true ; done
x=$(($x + 1))
printf "%d Echoed\n" $x
echo $x > $HOME/count

NOTE: Switch between virtual terminals with SHIFT+ALT+1 .. SHIFT+ALT+8 and back

 ~

CRON

  1. Read [1] [2].

  2. Understand examples from [3].

  3. Check your crontab syntax with [5].

NOTE!: "Command that runs from a crontab do not execute within a login shell. Workaround: explicitly call login shell" [1]:

* * * * * sh -l -c /home/user/myscript.pl

-l -- Make sh act as if it had been invoked as a login shell.

-c -- Read commands from the command_string operand instead of from the standard input. Special parameter $0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

 ~

SYSLOG

To make everything such as errors, notices, debug, warning messages become instantly logging towards above added new /dev/tty8.

  1. Identify which syslog daemon is used (syslog, rsyslog, syslog-ng):

    systemctl list-units --type=service | grep syslog
    rsyslog.service  loaded active running  System Logging Service
    
  2. Open /etc/rsyslog.conf and to the end of the file append below line :

    daemon,mail.*;\
    <tab>news.=crit;news.=err;news.=notice;\
    <tab>*.=debug;*.=info;\
    <tab>*.=notice;*.=warn   /dev/tty8
    

    To make rsyslog load its new config restart it:

    # systemctl restart rsyslog
    
  3. Force to write something into /var/log/syslog

    # logger HELLO!
    # cat /var/log/syslog | tail -n 1
    Feb  9 15:09:53 bull-unattended it: HELLO!
    

    Message should also be displayed on /dev/tty8.

 ~

CRON LAB Example:

cron-kirill.log

it@bull-unattended:~$ sudo nano /etc/default/console-setup
it@bull-unattended:~$ sudo reboot
login as: it
it@172.25.69.102's password:
Linux bull-unattended 5.10.0-21-amd64 #1 SMP 
  Debian 5.10.162-1 (2023-01-21) x86_64

$ echo 456 | sudo tee /dev/tty8 > /dev/null
$ sudo -i
# pwd
/root
# nano counter
# chmod +x counter
# ./counter
cat: count: No such file or directory
1 Echoed
# ./counter
2 Echoed
# ./counter
3 Echoed
# cat count
3
# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
# cat counter
#!/bin/bash

for x in $(cat count) ; do true ; done
x=$(($x + 1))
printf "%d Echoed\n" $x
echo $x > $HOME/count
# chmod +x counter
# ls -l counter
-rwxr-xr-x 1 root root 112 Feb  9 15:35 counter
# ./counter
cat: count: No such file or directory
1 Echoed
# ./counter
2 Echoed
# systemctl status cron --no-pager --full
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; 
             enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-02-08 
             11:00:07 UTC; 30min ago
       Docs: man:cron(8)
   Main PID: 413 (cron)
      Tasks: 1 (limit: 2259)
     Memory: 456.0K
        CPU: 9ms
     CGroup: /system.slice/cron.service
             └─413 /usr/sbin/cron -f

--
*) Usually you have to press q to exit systemctl status.. . As steeldriver noted in comments: use --no-pager if you do not want this behaviour

https://askubuntu.com/questions/747156/how-to-avoid-horizontal-scrolling-in-systemctl-status/747158#747158

 ~

LINKS

  1. https://mediatemple.net/blog/news /complete-beginners-guide-cron-part-1/

  2. https://mediatemple.net/blog/tips /complete-beginners-guide-cron-part-2/

  3. http://www.nncron.ru/nncronlt/help/RU/working/cron-format.htm

  4. http://www.microhowto.info/howto /persistently_set_the_value_of_an_environment_ variable_for_all_users.html

  5. Check crontab online:

    https://crontab.guru/

  6. Enabling every rsyslog handled message to log to Physical TTY8 https://www.pc-freak.net/blog/set-logs-log-physical-console-devtty12-tty12-linux/