Title: Resolving Two Common crontab Issues

When working with crontab on Linux systems, users may encounter several issues. This note documents two common problems and provides solutions for each.

Problem 1: Errors when editing crontab with crontab -e

Users may encounter a series of errors when trying to edit their crontab file using the crontab -e command. These errors often look like this:

Error detected while processing /usr/share/vim/vim82/filetype.vim:
line   10:
E319: Sorry, the command is not available in this version: let did_load_filetypes = 1
line   13:
E319: Sorry, the command is not available in this version: let s:cpo_save = &cpo
line   47:
E319: Sorry, the command is not available in this version: func! s:StarSetf(ft)
line   51:
E319: Sorry, the command is not available in this version: endfunc

This issue typically arises because Ubuntu (and possibly other distributions) sometimes defaults to using vim.tiny, a minimal version of the Vim editor. vim.tiny lacks many of the advanced features and functions present in the full Vim version, leading to these errors when crontab attempts to use Vim’s filetype handling scripts.

Solution:

  1. Install the full Vim package: To resolve this, install the complete version of Vim using the following command:

    sudo apt install vim
    
    
  2. Select Vim as the default editor: After installing Vim, ensure it’s selected as the default editor for crontab. Use the select-editor command:

    select-editor
    
    

    Choose the vim.basic option from the list. This configures the system to use the full Vim version when editing crontab files.

After these steps, editing the crontab file with crontab -e should work without errors.

Problem 2: crontab scripts not loading environment variables from .bash_profile

Scripts executed by crontab often fail to access environment variables defined in the user’s .bash_profile (or .bashrc) file. This happens because crontab runs scripts in a non-interactive, non-login shell environment. The .bash_profile file is typically sourced only by interactive login shells.

Solution:

To ensure that your crontab scripts have access to the necessary environment variables, explicitly source the .bash_profile file within the crontab entry itself.

Modify your crontab entries to include the following pattern:

0 0 * * * . /home/user/.bash_profile; /path/to/script

Explanation:

By sourcing the .bash_profile before running your script, you ensure that all the necessary environment variables are available to the script when it’s executed by crontab.