Posted in Linux commands

Make temporary Files or directories

Sometimes I need a temporary file or a temporary directory with a unique name in my scripts. Of course I can create a file in /tmp with the actual time in its name. But what if some other user has the same idea, and the scripts run at the same time? Here comes mktemp.

Continue reading “Make temporary Files or directories”

Posted in Linux commands

ssh Keys

ssh and scp are the usual programs to login to a remote host and to copy files from or to a remote host. Normally users just start the program and type their password. But this is not very convenient, and even impossible, if these programs are used in a script which has to run automatically, e.g. as a cron job.
Continue reading “ssh Keys”

Posted in Linux commands

systemd, Part I

For my sensor network I run a server process, which collects the data from several sensors. This is a sort of a daemon. In a first version it was started in a crontab with the time “@reboot”. But during the development I had to restart it every now and then: search the PID, kill it, start it in the background.

Isn’t there a better way? systemd to the rescue. In this first systemd post I will install a service file, so that the process can be started and stopped from the root account.

Continue reading “systemd, Part I”

Posted in Linux commands

“find” with Style, Part 3: Displaying File Attributes

If you search files with the find command, you get a list of filenames:

$ find . -name '*.py'

If you need the file attributes (like size, file permissions, …) you can do some tricks to let ls do the job. Or do you?

find has the parameter -ls which does this:

$ find . -name '*.py' -ls

Posted in Linux commands

Following log files

tail is a well known command to show the last lines of a file:

$ tail bmp280.log
2017-04-02T11:32:03 1491125523 20.62 1014.21
2017-04-02T11:33:02 1491125582 20.63 1014.23
2017-04-02T11:34:03 1491125643 20.62 1014.23
2017-04-02T11:35:03 1491125703 20.62 1014.23
2017-04-02T11:36:02 1491125762 20.63 1014.19
2017-04-02T11:37:03 1491125823 20.63 1014.23
2017-04-02T11:38:02 1491125882 20.63 1014.24
2017-04-02T11:39:04 1491125944 20.63 1014.25
2017-04-02T11:40:03 1491126003 20.63 1014.26
2017-04-02T11:41:02 1491126062 20.64 1014.27

If the default of ten lines is too few or too much you can adjust this with the parameter -n:

$ tail -n 25 bmp280.log

But the real power of tail are the parameters -f und -F. These parameters follow the end of the file while the file grows:

$ tail -f *.log
==> bmp280.log <==
2017-04-02T11:39:04 1491125944 20.63 1014.25
==> temp_humi.log <==
2017-04-02T11:39:05 1473343255 26.36 63.13 1 3.06
==> bmp280.log <==
2017-04-02T11:40:04 1491126004 20.63 1014.25
==> temp_humi.log <==
2017-04-02T11:40:05 1491126005 26.36 63.13 1 3.06

If the file does not exist yet, the parameter -F tells tail to wait until the file exists:

$ tail -F sensor_01.log
tail: cannot open ‘sensor_01.log’ for reading: No such file or directory

The message is just a warning. As soon as the log file is written, the lines appear.

Sometimes the lines with the filenames are difficult to find. But here grep is your friend:

$ tail -f *.log | grep -E --color '|==>.*<=='

Now the lines with the file names are coloured.