Skip to main content

19 posts tagged with "linux"

View All Tags

Date created: 2024-10-02

Go wild here

Given an awk script script.awk, an input file source.csv, and an output file target.js, we can execute awk like the following:

$ awk -f script.awk ./source.csv > target.js

Date created: 2025-02-14

Go wild here

This allows for sed to operate on multiline input, i.e. an entire file or multiline console output. For those pesky times when you want to use it to replace a new line symbol with a comma and a space for example.

sed ':a;N;$!ba;s/\n/ /g' file

Arguments

- :a create a label 'a'
- N append the next line to the pattern space
- $! if not the last line, ba branch (go to) label 'a'
- s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can)

Date created: 2024-10-10

Resources and links

https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html

Go wild here

some.service
[Unit]
Description=Some service template
After=network.target # Means "start when network ready".
# Can be replaced with the name of some other service to wait for.
StartLimitIntervalSec=0

[Service]
Type=simple # One of simple, exec, forking, oneshot, dbus, notify, notify-reload, or idle.
Restart=always # One of always, on-failure, on-success, on-abnormal, on-watchdog, on-abort.
RestartSec=1 # Time to sleep before attempting a restart.
User=kblagoev # User to run service under.
ExecStart=/usr/bin/env php /path/to/server.php # The actual command to run.

[Install]
WantedBy=multi-user.target

To make use of it:

$ cp <some.service> /etc/systemd/system/<some.service>
$ systemctl start <some.service>
$ systemctl enable <some.service> # This will ensure automatic start on boot

Notes

Restart limit

By default, systemd gives up restarting the service if it fails to start more than 5 times within 10 seconds. This is defined in the following variables

[Unit]
StartLimitBurst=5
StartLimitIntervalSec=10

This is avoided by setting StartLimitIntervalSec=0. This will assure systemd attempts restarting forever. The idea is that, as long as StartLimitIntervalSec is less than RestartSec * StartLimitBurst, the service will be restarted indefinitely.

As an alternative, you can leave the default settings, and ask systemd to restart your service if the start limit is reached, using StartLimitAction=reboot.

Limited automatic restart with a valid StartLimitIntervalSec

https://serverfault.com/questions/736624/systemd-service-automatic-restart-after-startlimitinterval

To have a service restart 3 times at 90 second intervals include the following lines in your systemd service file:

[Unit]
StartLimitIntervalSec=400
StartLimitBurst=3
[Service]
Restart=always
RestartSec=90

Date created: 2024-10-06

Description

Ubuntu supports .deb packages for installation. This allows to convert .rpm packages into .deb, and install them on Ubuntu.

Go wild here

sudo add-apt-repository universe #Add the Universe Repository
sudo apt-get update
sudo apt-get install alien #Install Alien package
sudo alien <name of package>.rpm #Convert .rpm package to .deb
sudo dpkg -i <name of package>.deb #Install the Converted Package

Or directly install .rpm with alien:

sudo alien -i <name of package>.rpm    #Install RPM Package Directly Onto the System on Ubuntu

Date created: 2024-09-23

Resources and links

https://stackoverflow.com/questions/57335266/how-to-untar-a-file

If a file ends on .tar.gz

tar -xzvf <filename.tar.gz>

If you want to specify a directory to extract to:

tar  -xzvf  <filename.tar.gz>   -C  <output/dir>

Where,

  • -z : Work on gzip compression automatically when reading archives.
  • -x : Extract tar.gz archive.
  • -v : Produce verbose output (Display progress and extracted file list on screen).
  • -f : Read the archive from the archive to the specified file. (In this case, read example.tar.gz.)
  • -t : List the files in the archive.
  • -r : Append files to the end of the tarball.
  • –delete (GNU/Linux tar only) : Delete files from the tarball.

Date created: 2024-09-23

Resources and links

https://stackoverflow.com/questions/57335266/how-to-untar-a-file

If a file ends on .tar.xz

tar -xjvf <filename.tar.xz>

If you want to specify a directory to extract to:

tar  -xjvf  <filename.tar.xz>   -C  <output/dir>

Where,

  • -z : Work on bzip2 compression automatically when reading archives.
  • -x : Extract tar.xz archive.
  • -v : Produce verbose output (Display progress and extracted file list on screen).
  • -f : Read the archive from the archive to the specified file. (In this case, read example.tar.xz.)
  • -t : List the files in the archive.
  • -r : Append files to the end of the tarball.
  • –delete (GNU/Linux tar only) : Delete files from the tarball.

Description

Directly from the systemd documentation

A unit configuration file whose name ends in ".service" encodes information about a process controlled and supervised by systemd.

Below is an example of how I'd set one up

Go wild here

some.service
[Unit]
Description=Some service template
After=network.target # Means "start when network ready".
# Can be replaced with the name of some other service
# to wait for.
StartLimitIntervalSec=0

[Service]
Type=simple # One of simple, exec, forking, oneshot, dbus, notify,
# notify-reload, or idle.
Restart=always # One of always, on-failure, on-success, on-abnormal,
# on-watchdog, on-abort.
RestartSec=1 # Time to sleep before attempting a restart.
User=kblagoev # User to run service under.
ExecStart=/usr/bin/env node /path/to/server.js # The actual command to run.

[Install]
WantedBy=multi-user.target

To make use of it:

$ cp <some.service> /etc/systemd/system/<some.service>
$ systemctl start <some.service>
$ systemctl enable <some.service> # This will ensure automatic start on boot

Notes

Restart limit

By default, systemd gives up restarting the service if it fails to start more than 5 times within 10 seconds. This is defined in the following variables

[Unit]
StartLimitBurst=5
StartLimitIntervalSec=10

This is avoided by setting StartLimitIntervalSec=0. This will assure systemd attempts restarting forever. The idea is that, as long as StartLimitIntervalSec is less than RestartSec * StartLimitBurst, the service will be restarted indefinitely.

As an alternative, you can leave the default settings, and ask systemd to restart your service if the start limit is reached, using StartLimitAction=reboot.

Limited automatic restart with a valid StartLimitIntervalSec

To have a service restart 3 times at 90 second intervals include the following lines in your systemd service file:

[Unit]
StartLimitIntervalSec=400
StartLimitBurst=3
[Service]
Restart=always
RestartSec=90