Blogs

Linux - Scripts, Cron, Screen, and Bash

SUMMARY
I tend to run various scripts. Some of them I would like to run as my normal user (meaning not root), and also make sure they launch whenever my Fedora server is rebooted. One of the last requirements, is also launching the script/application into a screen session, so that I can SSH in throughout the day, and check up on things. All of these requirements presented some various obstacles here and there. Basically, no matter what I did, I always ended up with a shell that wasn't a "complete shell". Meaning, I didn't get my custom prompt when I would SSH/screen in, or the $PATH was all messed up, so various things were not launching correctly. A bunch of other oddities. The solution, once I discovered all the pieces, was actually very easy. However, since it took some research, I wanted to make sure I wrote a little something about the end results.

 

PROCESS
First, I had this particular app that I wanted to make sure was running all the time. I'll call the application lupapp.

So, I write a typical launcher script for cron. First, let's just look at the launch script. We will call this script lupapp-launcher.sh

#!/bin/bash -l

/usr/bin/screen -X -S lupapp quit
/usr/bin/screen -dmS lupapp
/usr/bin/screen -S lupapp -X stuff "^M/home/danny/bin/lupapp^M"

 

The above has some key pieces that make the launcher work exactly as expected. Let's break down each line, one by one.

#!/bin/bash -l

This first line launches my lupapp script/application into a fully functional shell. This simple process of "bash -l" was a key part in my research. It's simple, true, but was the exact component that got a lot of things working correctly for me. I have to say "Thank you" to this post: http://blog.endpoint.com/2013/05/login-shells-in-scripts-called-from-cron.html

 

/usr/bin/screen -X -S lupapp quit

This line is clearing out and removing any previous screen sessions with the same name, lupapp. Always a good idea to start clean.

 

/usr/bin/screen -dmS lupapp

This line is launching the new screen session in a detached mode, and naming the session lupapp. The man page for screen has the perfect description: Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

 

/usr/bin/screen -S lupapp -X stuff "^M/home/danny/bin/lupapp^M"

Again, this line comes from the results of the research The "stuff" and the "^M" is what is doing the magic for me. Originally, I could get screen to launch my sessions just fine. I could even get it to push some lines to the shell. But, I just couldn't get it to "hit enter", and actually launch my application. That's exactly what this line does for me.

 

So, the above is what really gets my lupapp launched into the exact screen session I want. The only other part is getting cron to launch this for me on reboot. The crontab entry that seems to be consistently working for me is:

@reboot /home/danny/bin/lupapp-launcher.sh &> /dev/null

 

CONCLUSION
All of this seems pretty simple now. But, like I said, I initially was coming up against various issues that didn't produce the exact results I was looking for. With the above, I just make sure I backup my /var/spool/cron/danny file, and obviously everything in my /home/danny/bin directory. That is all that is really needed, and my scripts and apps are always available to me, even after a remote reboot.

Pages

Subscribe to RSS - blogs