Hi there AppWorks fans,
Welcome to a new installment of AppWorks tips.
I got this comment in my post “Cleaning up runtime mess #RTFM”
1 | For our 22.1 CE, I was working on integrating Archiving tool. It is currently |
“crontab” is unfamiliar terrain for me, so let’s quickly gain some knowledge in the form of a post. It’s not specifically required for the AppWorks platform as it’s more a Unix thingy; BUT as I also like to automate things with the Command Line Interface (CLI) of the platform in combination with CI/CD it’s worth the time to learn something new!
Let get right into it…
Let’s do a Google first on what on earth is a "crontab" in Linux
; It’s a short for “Cron Table” and just a simple file to schedule “Cron Entries” (a list of actions to execute…also known as “Cron Jobs”) to run on specified times! Sound like the ‘Task Scheduler’ in Windows…
With these Google entries, I also see these related terms passing by:
- “Cron”: In the Greek language, “cron” means “Time”…Aha! ⏱
- “Cron Job”: Is the command that a “Cron Daemon” will run at intervals; Also known as a “Cron Schedule” that includes the instructions about the commands to execute and when that should happen.
- “Cron Daemon”: A “Daemon” in Unix is just a background process starting at system boot and running until the system is down; Remember
systemctl daemon-reload
after a service creation!? Aha…AGAIN! 😎
This is my resource.
So, in short…We create a ‘cron table’ file, containing ‘cron jobs’, executed by the ‘cron daemon’…How hard can it be!?
A practical demo example to play with
So, that’s the theory…What does it mean in practice? For this I start at the terminal of my Unix (RHEL) machine and just try the command crontab
and hit <Enter>
. Nothing happens? Or are we now in a cronfile/crontable?? Let’s kill it with <Ctrl>+<c>
.
You can also use
<Ctrl>+<d>
to kill it with a validation message! We’ll keep that one in mind.
Let try something else first:
1 | [sysadmin@appworks ~]$ crontab -V |
Does this mean sudo crontab -l -u sysadmin
is the same? YES, it is!
Time for a first creation with sudo crontab -e -u sysadmin
. This starts the ‘vi’ editor with a /tmp/crontab.{ID}
file…
Quick vi-reference
- Hit
<Insert>
to start making changed- Hit
<Esc>
to stop making changed- To save the file (after
<Esc>
) type::w
- To quit the file (after
<Esc>
) type::q
I just type (or insert) Test
, hit the <Esc>
key and type :wq
with an error as result:
1 | crontab: installing new crontab |
Don’t be smart to stay in the file with only
:w
…You really need to:q
it!
Give y|n
to continue, but it looks something magic validates and triggers in the backend! Interesting!?
I give y
, and this time we try it with input (a “Cron Job”) like this:
1 | * * * * * echo The current date is `date`. |
This triggers every minute the Unix command after the asterisks; If you want to understand the asterisks, have a look for examples at crontab.guru…A great site!
After the save (and validation), you’ll find it in sudo vi /var/spool/cron/sysadmin
…
Now what? Let’s check with sudo crontab -l -u sysadmin
or crontab -l
for the current user! You should see an entry passing by now.
Great…How to run it and where do we see the results?
Back to the single
crontab
command that looks like nothing happens…Well, it does! Write something valid like* * * * * echo Hello world...
and hit<Ctrl>+<d>
; See that it overwrites the ‘crontab’ for the current user! Check withsudo crontab -l -u sysadmin
or justcrontab -l
Run the crontab and check the outcome
Well, after some research it looks like the crontab is directly active and is running based on its schedules (the “Cron Jobs”). From my RHEL VM (depends on your Unix distribution!), I get my first results via journalctl -t CROND
, but later I also start to monitor this file: sudo tail -999f /var/log/cron
My next question would be…Can we pause the scheduled runs!? Yes, by stopping the “Cron Daemon”: systemctl stop crond
Aha!…Now it starts to “snap”! CRON is a rather simple principle; I just needed to play with it once! NEXT…
Call the AppWorks CLI over a crontab
Now that we know how to schedule an echo
command, it’s time to move on with a simple CLI AppWorks call:
1 | * * * * * cd /opt/opentext/AppWorksPlatform/defaultInst/bin; ./cws.sh help |
Use a
;
to do multiple commands!
After my save I start to see this in my logging:
1 | sudo: a terminal is required to read the password; either use the -S option |
I give it a second FAILED try with: * * * * * cd /opt/opentext/AppWorksPlatformCE/defaultInst/bin; ./cws.sh help -S admin
Next try: * * * * * cd /opt/opentext/AppWorksPlatformCE/defaultInst/bin; sudo -S ./cws.sh help < password.secret
password.secret
is a new file (in the/bin
folder for now) containing my sudo account (sysadmin) password; In my caseadmin
Does it work? Ohw YES, it works!…Is it smart? Well, that’s more a security type of question that I want to avoid in this post. I did a quick search on “Safest way to inject sudo command with password in Linux”…Well, that’s a new world to dive into! Out of scope for AppWorks…Ask your Linux expert! 😎
For me, it’s a “DONE” where we did a quick smell on the terminology “CRON” in scope of Linux; Why? Because it’s in line with the CLI and CI/CD principles of the platform AND because we simply like to learn new stuff. Have a great weekend; I CU next week on another topic on AppWorks Tips.
Don’t forget to subscribe to get updates on the activities happening on this site. Have you noticed the quiz where you find out if you are also “The AppWorks guy”?