12

I'm trying to edit my crontab, but I just can't open it!

So with my user foo, I just type:

crontab -e

Then I got:

no crontab for foo - using an empty one
nano: No such file or directory
crontab: "nano" exited with status 1

So I tried first:

export EDITOR=nano

I retried and I got exactly the same output. I also tried to set my editor to Vim with:

export EDITOR=vim

no crontab for foo - using an empty one
vim: No such file or directory
crontab: "vim" exited with status 1

But I keep getting the same output again and again. How can I open my crontab and then edit it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hito
  • 697
  • 1
  • 8
  • 23

9 Answers9

16

This message is normal because you still do not have any crontab for that user:

no crontab for foo - using an empty one

Regarding the following:

nano / vim: No such file or directory

crontab: "nano" exited with status 1

It is happening because you are not defining the editor properly. To do so, you have to specify the full path of the binary:

export EDITOR=/usr/bin/nano

or

export EDITOR=/usr/bin/vi
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Yeah but I also tried to set my editor with the 2 lines you gave above. Still doesn't work. – Hito Feb 18 '14 at 09:20
  • Well first check what is the path of your `nano` with `which nano`. My `/usr/bin/nano` is just an example. – fedorqui Feb 18 '14 at 09:22
  • I also did it and it gave me /usr/bin/nano too. – Hito Feb 18 '14 at 09:32
  • And you are still getting the "no such file or directory"? – fedorqui Feb 18 '14 at 09:33
  • Yes. To summarize, I did : `EDITOR=/usr/bin/nano` `export EDITOR` `crontab -e` and I'm still getting : `/usr/bin/nano: No such file or directory` – Hito Feb 18 '14 at 09:36
  • If you use `/usr/bin/nano` in a normal basis, does it work to you? I mean, if you just execute `/usr/bin/nano`, is it opening? – fedorqui Feb 18 '14 at 09:37
  • Yes it does. Same with vim and vi by the way. – Hito Feb 18 '14 at 09:40
  • If you cannot use these binaries in general (not only when opening crontab), then the problem is not with crontab but with nano / vim. – fedorqui Feb 18 '14 at 09:43
  • And what about nano? Have you tried reinstalling it? – fedorqui Feb 18 '14 at 10:45
  • Yes I did. I can't understand why it doesn't work. It's not the first time I use crontab and I'm kinda sure that my settings are OK. Is it possible to create/edit a crontab file without using the command crontab -e ? – Hito Feb 18 '14 at 10:56
  • Well you can try to reinstall crontab. But I insist that if nano/vim are not working to you at all (not only on crontab), then the problem is not in crontab. – fedorqui Feb 18 '14 at 11:04
  • This will not work for sudo crontab -e when you need to run something on the root's crontab. For that, and a full-spectrum solution, see my answer. – Theodore R. Smith Oct 02 '20 at 04:43
6

I was getting the exact same errors on my new EC2 instance.

no crontab for ec2-user - using an empty one
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127

Vim was working fine when you open it, but crontab -e was still not working. I then tried the solution:

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export EDITOR=/usr/bin/vi

which did not work, and then I typed:

which vi
alias vi='vim'
/usr/bin/vim

What did the trick for me was using Vim instead of vi on the export:

export EDITOR=/usr/bin/vim

Now my crontab -e works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean
  • 267
  • 4
  • 9
  • How do I make this export permanent, so that I don't have to export it with every new terminal session. – Shri Sep 30 '16 at 08:11
  • This will not work for `sudo crontab -e` when you need to run something on the root's crontab. For that, and a full-spectrum solution, see my answer. – Theodore R. Smith Oct 02 '20 at 04:43
1

Literally none of these solutions worked for me on modern Arch Linux when using sudo.

sudo crontab -e # Didn't work
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127

sudo -s
crontab -e # The same error

su -
crontab -e # The same issue!!!

ssh root@localhost # The same thing :O

Even logging in as root from the CLI (Ctrl + Alt + F2) didn't work...

So I was beginning to suspect it was just broken.

Well, I solved the ssh root@ and CLI issues in both bash and zsh by adding

EDITOR=/usr/bin/vim

to the very bottom of /etc/profile.

I kept poking around and I found the hidden secret that's befuddling everyone here, especially Hito's answer!

sudo visudo

And add the following:

Defaults env_keep += "EDITOR"

Now, it's all going to work! sudo crontab -e, sudo -s; crontab -e, etc. I bet it fixes the problem with other applications as well!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
1

Use the env program to pass environment variables to crontab.

sudo env EDITOR=nano crontab -e

It also works without sudo.

I haven't found a way to do it permanently, but this works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

As stated above, you may have nano installed in a different location, or in a location that isn't in your PATH variable. As an alternative, you can try, which pico, and if "pico" shows up with that command, then follow the procedure of export EDITOR=pico, followed by crontab -e.

Remember to include the export line in your shell's start-up script, or the command line entered EDITOR value will go away when you leave your terminal session.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Sardo
  • 39
  • 3
  • I also tried this and I still get the same output... I added export EDITOR=vim in my .zshrc – Hito Feb 18 '14 at 09:33
  • So with the 'export EDITOR=vim' in your .zshc, you are still having the problem you listed in your question? – Nick Sardo Feb 18 '14 at 09:37
  • Yes I do. I source the .zshrc and also launched a new session to be sure that it's correctly loaded. if I type `echo $EDITOR` it answers `vim` – Hito Feb 18 '14 at 09:43
  • glad you got it fixed. I could find nothing to indicate why you had this problem. If the editors work without crontab, then as was previously stated in the other thread, the problem must have been with your installation of crontab. Regardless, you've got a solution working, and that's what's important. – Nick Sardo Feb 18 '14 at 11:24
0

To make Sublime my default editor, I started with this line in my .bashrc:

# In .bashrc
export EDITOR="subl -w"

The result:

> source ~/.bashrc
> crontab -e
crontab: subl -w: No such file or directory
crontab: "subl -w" exited with status 1

I tried fully qualifying the path to the soft link:

> which subl
/usr/local/bin/subl
> ls -la /usr/local/bin/subl
... /usr/local/bin/subl -> /Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl


# In .bashrc
export EDITOR="/usr/local/bin/subl -w"

The result:

> source ~/.bashrc
> crontab -e
crontab: /usr/local/bin/subl -w: No such file or directory
crontab: "/usr/local/bin/subl -w" exited with status 1

Finally I removed the -w:

# In .bashrc
export EDITOR="/usr/local/bin/subl"

Which worked:

> source ~/.bashrc
> crontab -e
# opened Sublime

I was able to make nano work using the same steps.

Amin Ariana
  • 4,635
  • 2
  • 35
  • 21
0

For me, the problem was that /usr/bin/vi wasn't installed. I installed it with

apt install vim-tiny

I could then access the editor with

crontab -e
NicklasF
  • 863
  • 3
  • 10
  • 26
-1

I couldn't find a fix for my problem, but I really have to be able to execute some crons. So after googling, I could find that cronie isn't the only cron manager existing. fcron is my solution. So I did:

pacman -R cronie
pacman -S fcron

Then instead of crontab -e, just type fcrontab -e, edit your file, and you're good. It works like a charm for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hito
  • 697
  • 1
  • 8
  • 23
-1

My solution was to export using the absolute path of my preferred text editor in my .xprofile.

export EDITOR=/usr/bin/vim

I then sourced it into my .bashrc with:

[[ -f .xprofile ]] && . .xprofile

This will make the environment variable persist throughout every shell session.

In my case, I'm using cronie. By default it looks for /usr/bin/vi and produces

no crontab for user - using an empty one
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127

until you set an $EDITOR environment variable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jnlar
  • 1
  • 4
  • It's still virtually impossible to edit the root's crontab with this partial solution. See my answer for a complete solution. – Theodore R. Smith Oct 02 '20 at 04:39
  • Interesting.. when I use `sudo crontab -e` with the export line noted in my solution I'm able to modify roots crontab – jnlar Apr 03 '21 at 02:10