Monthly Archives: May 2018

cron

In the last post we talked about scheduling things one at a time, which doesn’t work if we have things we want to repeat. Like I want my computer to make a beep every hour on the hour, I can either sit down and repeatedly run the at command until I’m sick of running the at command, or I can use a different system. That different system is cron.

That’s a weird name for a time keeping system. The story is (and who knows if this is true) it was named after the Greek god of time, Cronus.

Cronus

Cronus

Cron is very flexible. You can run things every hour, every day, every month, every week, every 5 minutes, thirty minutes after the hour, you get the idea. Any kind of regular time interval you want, you can run your program. It’s managed by a file called crontab.

Every user on a Linux system can operate a crontab, there’s no restrictions on that. It has a format you have to follow, otherwise it won’t work. The first five entries in the file are time related.
These  entries are minute, hour, day of month, month, and day of week.  Each of them have values you can put in:

field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

If I want something to run at midnight, then the first field is 0, the second is 0, but what about the third? I can use a ‘*’ to mean a wildcard. This means that on any day of the month, the job will run if the time matches the first two fields. I want it to run at midnight every day, so I put a * in the last two fields. If I want my computer to beep every night at midnight, my crontab would look like:

0 0 * * * /home/computerlamp/beep.sh

That program looks weird, right? Well in order for them to work, you have to give the full path of the program, otherwise the computer doesn’t know where to look.  Just like with at!

We’ll look at more examples of crontab next time.

Linux Schedule

People schedule things for all sorts of reason. There’s school schedules, lunch schedules… I bet Iron Man has a schedule for cleaning his suit. Which makes me wonder, what does Iron Man do for a bathroom if he’s in the suit for a long time?

Anyway, back to scheduling. Suppose I want my computer to make a beep in two hours to remind me to go watch The Avengers.It’s very important that I start watching in two hours because I want to follow it up with The Avengers: Age of Ultron and then I want to go have dinner and see Infinity War.

The Avengers!

The Avengers!

I want my computer to make a beep in two hours. The first question is, how do I make my computer beep?

One way to do it is by this:

Alpha:~ computerlamp$ echo -en "\a"

 

That’s a lot to remember, isn’t it? Let’s use nano and make a file we’ll call beep.sh

Here’s a picture of me making the file:

Making beep.sh

Making beep.sh

Remember, when you save it, call it beep.sh

The next thing to do is to change the permissions on the file. We want this to be executable.

Alpha:~ computerlamp$ chmod 755 beep.sh

Now we’ve made our first shell script. A shell script is a computer program that executes shell commands in the order that they’re in the file. So if we run beep.sh like this:

Alpha:~ computerlamp$ ./beep.sh

 

we’ll get a beep! You’ve now written a computer program. How cool is that?

Now the next problem is how do we schedule this?

We use a program called at. I know, that’s a boring name. Now it’s 11 am and I want the computer to beep me at 1pm.

Alpha:~ computerlamp$ at -f /home/computerlamp/beep.sh 01:00PM

 

Now my computer knows that at 1PM it should run the program beep.sh, which means my computer will beep at me then.

How does this work? Well, there’s a daemon called atd that runs these jobs.

What if I want to run something every hour on the hour? I want to make my own cuckoo clock by making my computer beep at me every hour. We’ll talk about how tod o that next time.

Daemons

Linux® has a lot of processes that run in the background. This means that they don’t disturb you when they run, they sit back there and, well, run. For example, the syslog process, the process that sends email, init, the great granddaddy of processes, they all sit back there and do their job without bothering you. They’re actually all called daemons.

Daemon in the flesh

Daemon in the flesh

That little guy is kind of cute, isn’t he?

The name came from a thought experiment known as Maxwell’s demon. In this experiment, an imaginary daemon moved things around in the background. They also come from Greek mythology, where the daemon is a guy working in the background but who isn’t necessarily up to no good. He’s not necessarily up to good either, he’s just back there doing his thing. The Greek Mythology spelling is the one that stuck for the daemons on the system.  That little guy is the mascot of the BSD® operating system.

A general rule is that daemons on the system end their name with ‘d’ so you know they’re daemons. syslogd is the daemon for syslog, dhcpd is the daemon that runs dhcp, and named is the daemon that runs DNS. This isn’t always true though, one of the main programs that handle email is called sendmail and so is it’s daemon. There’s no d at the end of that one.

I ran apropos daemon to see what I’d get:

arpd (8) - userspace arp daemon.
authorized_keys (5) - OpenSSH SSH daemon
blkmapd (8) - pNFS block layout mapping daemon
cron (8) - daemon to execute scheduled commands (Vixie Cron)
daemon (1) - turns other processes into daemons
daemon.conf (5) - turns other processes into daemons
dbus-daemon (1) - Message bus daemon
epmd (1) - Erlang Port Mapper Daemonepmd [-d|-debug] [DbgExtra...] [-port No] [-daemon] [-relaxed_command_c...
faked (1) - daemon that remembers fake ownership/permissions of files manipulated by fakeroot processes.
faked-sysv (1) - daemon that remembers fake ownership/permissions of files manipulated by fakeroot processes.
faked-tcp (1) - daemon that remembers fake ownership/permissions of files manipulated by fakeroot processe

 

This isn’t all of the output, I just chose the first few lines. You should do it on your system to see what you’ll get but there’s a lot of results, aren’t there?

We’ll talk about some of these daemons in the future, because most of them are important to keep your Linux® system running happily.

Keylogger — Stealing your Typing for Fun and Profit

The last post about the last command got me think about security again and how people can get your password to log into your system.  Passwords on the Linux® system are one-way cryptography, so a bad guy shouldn’t be able to break it.  There is a tool a bad guy can use to get your password, it’s called a keylogger.

A key logger is a program that records every single key stroke you make.

A keyboard

A keyboard

Imagine someone sitting inside your keyboard and recording every command you type.  It could also record you entering your password and then it would have your password.

That’s a scary thought, right?   How do you get hit with key loggers?

They’re a virus.   Bad guys use them for more than just stealing your password, they’re also used for stealing credit card information and social security numbers.  They really want to steal money, your identification, and anything else they can sell.

They’re also often installed as a hardware addition.  The program is on a USB stick, is stuck into a slot on to your computer, and then the keylogger saves all it can onto the USB stick and waits for someone to come and get it.

If someone has physical access to your computer, they have all the time in the world to break into it.  They need the physical access to put the keylogger on, so all they have to do is walk up, slip it on, and walk away.  They do hope that you won’t find it.

If they steal the computer from you, they also have all the time in the world to get your information off of it. It’s one of the main tenets of computer security, keep your computer physically secure.  Imagine if a CEO’s laptop walked off?  CEOs have lots of information on their computers about the company, if the bad guy has it, then all that information is up for grabs.