I recently installed a new server in my home office. I typically just leave my servers to run headless. But with an old monitor laying around and plenty of idle CPU time I decided to play a bit. I mounted the monitor to my office rack and then started to work.
Rather than just display the normal text login prompt, I wanted it to show something cool at boot. I started to dig around on the web and found this article. It quickly described how to run a program before the login prompt on Ubuntu 16.04+.
Run Your Program before login
So I wrote a simple script /root/loginMatrix.sh which would simply run cmatrix on the main (tty1) console. Once I exited cmatrix it would display the normal login prompt. The sample script is as follows:
#!/bin/sh /usr/bin/cmatrix -abs exec /bin/login
I then edited the config file for getty@tty1 here (for Ubuntu 16.04+ only, not sure on other distrubutions):
/etc/systemd/system/getty@tty1.service.d/override.conf
I changed the contents to be:
[Service]
ExecStart=
ExecStart=-/root/loginMatrix.sh
StandardInput=tty
StandardOutput=tty
and then I ran the following command to activate it:
systemctl daemon-reload; systemctl restart getty@tty1.service
After the change the system started to show the cmatrix terminal animation immediately. But once I quit the application it was back to the login prompt.
Getting tricky
After running cmatrix for a few days straight, I decided that I wanted to change it up a bit. So I made a few adjustments to the /root/loginMatrix.sh script to make it a bit more dynamic. With the following changes I was now able to display something different each time I used the command prompt.
#!/bin/bash declare -a arr=("/usr/bin/cmatrix -abs" "/snap/bin/asciiquarium" "/usr/sbin/iftop" "/usr/bin/htop") size=${#arr[@]} index=$(($RANDOM % $size)) eval "${arr[$index]}" exec /bin/login
These changes told the script to randomly choose either, cmatrix, asciiquarium, iftop, or htop and execute it. Then as before once I quit the application that was randomly executed it would again display the login prompt. My kids got way to excited when asciiquarium was chosen and had to watch the fish swim by. This solution worked for a while, but eventually I got tired of having to change the displayed program manually. So I started playing with options to automate the program change.
Automating the switch
These changes got a bit trickier. The script had to track the application PID so it could kill it when the timeout was reached. After trying several different methods I finally ran across this basic method for timing out a process. And the process isn’t perfect, but it does rotate through the different options on a ten minute interval. So that works, but the exiting to the login prompt doesn’t. So it’s only most of the way there. Here is my current /root/loginMatrix.sh script:
#!/bin/bash declare -a arr=("/usr/bin/cmatrix -abs" "/snap/bin/asciiquarium" "/usr/sbin/iftop" "/usr/bin/cacafire") size=${#arr[@]} continue=1 timeout=600 interval=1 while [ $continue -eq 1 ] do index=$(($RANDOM % $size)) eval "${arr[$index]} &" cmdpid=$! ((t = timeout)) while ((t > 0)); do sleep 1 kill -0 $cmdpid || exit 0 ((t -= interval)) done exit_status=$? echo $exit_status > ext.txt if [[ $exit_status -ne 1 ]]; then continue=0 fi kill -s SIGTERM $cmdpid && kill -0 $cmdpid || exit 0 sleep 1 #kill -s SIGKILL $cmdpid done exec /bin/login
So this script accomplishes the switching of applications on the primary console. And I was able to add cacafire to the mix for a nice colored ascii fire animation. But if I have to use the console for the login, I will have to hit ctrl-alt-F2 and switch over to tty2. That won’t be the end of the world, lol. And in the meantime I have some fun console effects to keep my office interesting.
Did you like this article on how to run a program before the login prompt? If so you may like this article on how to change your hostname on Centos