Go to main content

Managing System Information, Processes, and Performance in Oracle® Solaris 11.4

Exit Print View

Updated: August 2021

Creating and Editing crontab Files

The simplest way to create a crontab file is to use the crontab -e command. This command invokes the text editor that has been defined for your system environment in the EDITOR environment variable. If this variable has not been set, the crontab command uses the ed editor.

The following example shows how to determine whether an editor has been defined, and sets up vi as the default editor.

$ which $EDITOR
$ export EDITOR

When you create a crontab file, it is automatically placed in the /var/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root if you are in the root role.

How to Create or Edit a crontab File

Before You Begin

If you are creating or editing a crontab file that belongs to another user, you must assume the root role. See Using Your Assigned Administrative Rights in Securing Users and Processes in Oracle Solaris 11.4.

You do not need to assume the root role to edit your own crontab file.

  1. Create a new crontab file, or edit an existing file.
    # crontab -e [username]

    where username specifies the name of the user's account for which you want to create or edit a crontab file. You can create your own crontab file without superuser privileges, but you must have superuser privileges to creating or edit a crontab file for root or another user.

    Caution  -  If you accidentally type the crontab command with no option, press the interrupt character for your editor. This enables you to quit without saving changes. If you instead save changes and exit the file, the existing crontab file will be overwritten with an empty file.

  2. Add command lines to the crontab file.

    Follow the syntax described in Syntax of crontab File Entries. The crontab file will be placed in the /var/spool/cron/crontabs directory.

  3. Verify your crontab file changes.
    # crontab -l [username]
Example 33  Creating a crontab File

The following example shows how to create a crontab file for another user.

# crontab -e mjane

The following command entry added to a new crontab file automatically removes any log files from Mary's home directory at 1:00 am every Sunday morning. Because the command entry does not redirect output, redirect characters are added to the command line after *.log. Doing so ensures that the command executes properly.

# This command helps clean up user accounts.
1 0 * * 0 rm /home/mjane/*.log > /dev/null 2>&1