0

I am using a fresh installation of Ubuntu 16.04 LTS.
Whenever I type:

sudo crontab -e

OR

sudo -u www-data crontab -e

The command prints some number and nothing else. And whatever you do to try to exit from it, by any means, you are not able to do.

Questions

  • First of all, How to exit from above command? I am just stuck.
  • Why the command is not showing the crontab entries?
  • How to make the above command to show crontab entries?

EDITED
I have chosen the editor as vim.basic with:

sudo update-alternatives --config editor

But when I do echo $EDITOR prints empty.

Also,
crontab -e -> works fine
sudo -u www-data crontab -e -> stuck as mentioned above

6
  • CROSS POST? This is the only place I have posted.
    – MagePsycho
    Commented Feb 24, 2019 at 7:46
  • IRC n Forums are two different entities.
    – MagePsycho
    Commented Feb 24, 2019 at 8:03
  • @MagePsycho If it responds to ESC and then : type of key sequence, then it's vim, so use ESC then : then q to exit. Commented Feb 24, 2019 at 8:19
  • Stupid question: Why didn't you install Ubuntu 18.04 LTS?
    – Freddy
    Commented Feb 24, 2019 at 8:43
  • This is in the production server. And I have running my sites on 16.04 LTS. Will try 18.04 though in future.
    – MagePsycho
    Commented Feb 24, 2019 at 8:57

4 Answers 4

4

I would guess that your default editor is not set and is defaulting to ed.

What do you get if you run:

env | egrep 'EDITOR|VISUAL'

sudo env | egrep 'EDITOR|VISUAL'

sudo -u www-data env | egrep 'EDITOR|VISUAL'

The way to quit from ed is to type: q↵ Return

To set a different editor, you could do:

sudo -u www-data EDITOR=$EDITOR crontab -e
0
3

First of all, How to exit from above command? I am just stuck.

We don't know what editor you are using. Try Esc :q (vim) or Ctrl-x (nano) or Ctrl-x Ctrl-c (emacs) or q (ed).

  • Run command select-editor and choose an editor you want to use
  • Alternative: set environment variable EDITOR or VISUAL to an editor of your liking.
    Example: export EDITOR=emacs to use emacs or export EDITOR=nano for nano.
  • Then, run crontab -e to edit your crontab
  • Use crontab -l to show the content of your crontab
6
  • I am logged in as sudo user. echo $EDITOR -> gives blank.
    – MagePsycho
    Commented Feb 24, 2019 at 8:04
  • Yes, that's the default. What does select-editor tell you?
    – Freddy
    Commented Feb 24, 2019 at 8:05
  • I have added more details. Please check.
    – MagePsycho
    Commented Feb 24, 2019 at 8:08
  • 1
    Login as www-data and add the environment variable to ~/.profile using echo -e "\nexport EDITOR=vim" >> ~/.profile. Then logout. Then try sudo -u www-data crontab -e again.
    – Freddy
    Commented Feb 24, 2019 at 8:21
  • I have many users for crontab entries. Is there a way to set the default for all (system wide)?
    – MagePsycho
    Commented Feb 24, 2019 at 8:23
1

I have applied the following commands

# Interactive way
update-alternatives --config editor
# And choose 3 (vim.basic)

# Non Interactive way
sudo update-alternatives --set editor /usr/bin/vim.basic

# Verify as
sudo update-alternatives --query editor

Alternatively, you can add it in ~/.bashrc or ~/.bash_profile file and reload to reflect the changes.

export VISUAL=vim
export EDITOR="$VISUAL"

For crontab for www-data user, I am using as:

sudo crontab -u www-data -e

And this fixed the issue.

0

Don't use crontab -e

  • unless you know exactly how your system, account and session are configured you don't know what happens next.
  • Change any one of these and something different can happen
  • even if it does open your crontab in an editor you can operate, it can be tricky to abandon the changes

Instead, write the crontab to temporary file, use the editor of your choice, import the file back into cron:

$ crontab -l >mycron
$ vi mycron
$ crontab <mycron

(I would also STRONGLY recommend not running cron jobs as the www-data user)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .