DEV Community

Bartlomiej
Bartlomiej

Posted on

The Journey #1 - Loggern't, Archiven't and Cron't

Hey there!

Starting The Journey with logger that didn't log and cronjob that didn't cron!

While not the very first thing I have learned during the course it is the very first thing I want to share - Linux: the logger, the archiver and the crontab

The reason I want to start with that is that it proves what I've said on my prior post - That while learning I tend to get sidetracked, but I gain something in return.

I've had some experience with Linux in university, but it being over 7 years ago is bound to create some gaps in the knowledge.
After regaining some knowledge on the basics and hearing about crontab somewhere (I really don't remember now πŸ˜…) I have thought to myself: let's write anything and put it in cron!

So my next thing was to put something that would have timestamps so I can actually check if something works periodically - and what wouldn't be better than creating some basic logger?

I need to mention that the version you will see is the product of some iterations so that might not look cohesive πŸ˜….

Lo and behold... [the current version of] logger
Actually the current version of logger

I've spent a short while writing a script, nothing too crazy going on.
But then came hooking it up to crontab, which I had no prior knowledge of.
I think you can already guess what happened... yea, didn't quite work

For tests purposes I've already used

chmod 555

So I just knew it had to work...
But I've learned the hard way how time works in cron - if you put 1 in minutes column it doesn't mean - do it every 1 minute but do it when minutes of an hour is equal to 1

How crontab works

So after gaining that knowledge I've waited some more and there it was, my first automated log!
Which would grow endlessly... So let's schedule archiving daily!

Archiver script

The open log file doesn't get reset, the logs aren't getting archived despite the script working when using it manually.
Now guess what issue I could have with that one...
Yea, it seems chmod is a really important thing, and remembering to use it is even more important.
When I think of it im pretty sure cron also has user, so instead of using chmod I could just use chown - so there is another takeway!
And oh look, here it is!

This one would grow a bit over time to provide more sophisticated methods, such as using tar to compress it into a tiny tarball.
And oh boy, look at the size difference πŸ‘€!

Did I follow beaten path? No
Did I fall down? Yes
Do I regret that? No
I have learned things I wouldn't just by clicking along with the course.

So what are they?

  • Always remember about chmod
  • It's even better to use chown
  • Cron is extremely powerful tool
  • tarballing is efficient
  • top is for humans ps is for scripts

See you in the next one!

Top comments (2)

Collapse
 
dk2233 profile image
daniel kucharski

Keep up the good work!

Collapse
 
wbartlomiej profile image
Bartlomiej

Sure will!