DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Useful Linux Commands — Environment Variables and Cron Jobs

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

env

The env command lets us set environment variables for the current session.

For instance, we can run:

env USER=username node app.js
Enter fullscreen mode Exit fullscreen mode

to set the USER environment variable in the current session.

We can clear environment variables with the -i switch:

env -i node app.js
Enter fullscreen mode Exit fullscreen mode

We can use the -u switch to make a variable inaccessible:

env -u USER node app.js
Enter fullscreen mode Exit fullscreen mode

Now the USER environment variable won’t be visible to programs in the session.

printenv

The printenv command lets us print environment variables onto the screen.

We can print a specific one by specifying it after the command.

For instance, we run:

printenv USER
Enter fullscreen mode Exit fullscreen mode

to print the value of the USER environment variable.

basename

The basename command lets us return the file name portion of the path.

For instance, we run:

basename /users/foo/test.txt
Enter fullscreen mode Exit fullscreen mode

to print test.txt onto the screen.

If the last segment is a directory, then we get that directory’s name.

dirname

The c command lets us get the direction portion of a path.

For instance, if we have:

dirname /users/foo/test.txt
Enter fullscreen mode Exit fullscreen mode

Then we get /users/foo displayed on the screen.

crontab

The crontab command lets us schedule cron jobs.

We can list the corn jobs scheduled with the -l switch:

crontab -l
Enter fullscreen mode Exit fullscreen mode

We can run:

crontab -e
Enter fullscreen mode Exit fullscreen mode

to add new cron jobs.

The syntax for setting the schedule for a cron job is the following.

To set the minute that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values

To set the hour that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 0–23

To set the day of the month that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 1–31

To set the month that the cron job runs, we can add one of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 1–12

To set the day of the week that the cron job runs, we can add one of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 0–6 — (0 is Sunday to 6 is Saturday)
  • SUN-SAT

For instance, we can run:

30 08 10 06 * /home/ramesh/full-backup
Enter fullscreen mode Exit fullscreen mode

to set the full-backup script to run at a specified time.

The expression means:

  • 30–30th Minute
  • 08–08 AM
  • 10–10th Day
  • 06–6th Month (June)
  • ***** — Every day of the week

Conclusion

We can use Linux commands to get and set environment variables and view cron jobs.

Top comments (0)