Automating execution with Cron tab!

As mentioned in a previous discussion I have recently started a Minecraft server. The purpose of this server was to not only have fun but to learn more about Linux. I have a good foundation but I want to continue growing that into a large city!

Anyways, the reason I mentioned Minecraft again is because I wanted to automate some scripts to upkeep my server. For instance, if I was away from the house and my internet cuts out how can I get it to turn back on? I also wanted another script to restart my server every night at a certain time when no one would be on to prevent any issues from growing.

To get started I would like to introduce the utility cron. It is a command-line utility and allows Linux users to schedule jobs to run on a pre-determined schedule. Similar to Windows with its use of Task Scheduler. The job can be as specific as the user or administrator desires. You can run it once a day at a certain time. You can even run it every minute of every day.

You can specify which user to apply the job for but an easy way to go about this is to log in with the user you want to apply it to and enter the command crontab -e. Doing so will open up the schedule and look similar to picture posted below.

It even gives you a good explanation on how to set up a job. As you can see from left to right you will edit the minute, hour, day of month, month, and day of week. If the value of the field does not matter then you use a * to specify “any”. That is really all that is needed.

The first job I created is with a script labeled as restartMC.sh. From left to right again you will notice that at 0 minutes at 0400 in the morning on every day of the month this script will run. This allows my server to refresh every night.

My second job is with a custom python script I developed. Very basic script but it gets the job done! Something I have not mentioned yet is seen in this example above “*/5”. The minute option on this job is specifying it to run every 5 minutes instead of at 5 minutes. So for every 5 minutes on every hour/day/month this script will run to verify my server is up and running.

A side discussion, I found that I really enjoy python and I noticed that python is heavily used today in the cyber industry especially for hacking and other fun stuff. So with that being said I bought a few books to better develop my skills (be on the lookout for those writeups in upcoming months) but to maybe help someone else in the future. Here is a snippet of the script I put together from some research online.

As I said very basic. Simply looks to see if the port that I specified under location variable is open. If it is not open then it calls the resartMC.sh script. Otherwise prints to screen “port is open” and then closes connection. Python is awesome!

Lastly I wanted to mention two more things regarding cron. If you wanted to edit a cron job across the entire Linux system then you would edit the /etc/crontab file. The biggest difference here is that a username is required to be specified. Here is another script I run on my server just to keep it up to date.

As you can see the user root is specified. it will run this updateme.sh script with root permissions. I run this script everyday at midnight. It is not a production server so I would rather a bug to happen because of a bad update or patch then leaving it unpatched. Last but not least you can also run the sudo crontab -e command and that should bring up another job scheduled with sudo permissions for that specific user.

Well that will be the end of todays discussion. I hope who ever decides to read this found some value. I always look for ways to automate or at least make my life easier. Especially in a lab environment where I can make mistakes and learn. When it comes time to work in a production environment I will have the necessary skills or at least an understanding on where to start.

As always, Never Stop Learning!