Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving cron job in ubuntu [closed]

Tags:

vi

cron

I am new to ubuntu and cron job. I have entered the following into my command line:

       crontab -e

and I get the following output: "no crontab for teddy - using an empty one 888"

Then I enter when I want it to execute (I believe this is right?... I want it to run once a day, everyday at 8pm):

                 00 18 * * * /*****/*****/****/test.php 

Here is my problem, I dont know how to exit back to the command line. Everything I type gives me weird looking letters and enter (return) doesn't do anything. I have read that this will do the job

                 ESC : w q

but its not working for me. I tried typing that in, I tried pressing them at the same time, I tried pressing one at a time. Nothing, still stuck. When I press ESC, it comes out as ^[.

This is probably very easy question and I apologize if it is stupid but I have been stuck for sometime. Any help would much appreciated.

Thank you

P.S. I read somewhere that if this is your first job that you need to to do an end line at the end of the cronjob... is this a simple enter key press or actually typing \n?

like image 650
Teddy13 Avatar asked Dec 16 '22 04:12

Teddy13


2 Answers

Teddy13, let's get some clarifications here.

Ubuntu is the distribution of Linux you are using. None of the commands you're typing are exclusive to Ubuntu.

You're asking questions about two separate issues. One is "How do I write a crontab". The other is "How do I use vi, the default editor of the crontab command".

First, man crontab to review the format for entries in the file. Note that cron runs things that are executable from the shell. You can only run your "test.php" script if it is structured like a shell script, with the first line containing "shell magic" (i.e. something like #!/usr/local/bin/php).

Second, while vi is a powerful and well-loved text editor, it's not the easiest to use. I fully support any efforts you may make to learn how to use it, but until you're comfortable with it, you might want to consider switching to "pico" or "ee" or "joe", which are all much easier to learn, though they can do much less. You can install joe, for example, with the command: apt-get install joe run as root. Then to use joe to edit your crontab, add export VISUAL=/usr/bin/joe to the .bashrc file in your home directory.

There's a lot of background information you may want to get. Read lots. It's worth it.

UPDATE (per comment):

Here's the basic stuff you need to edit your crontab.

  • crontab -e ... as you now know, this edits your crontab file using $EDITOR or $VISUAL, which defaults to vi.
    • Inside vi, you are always in one of three modes. MOVEMENT mode lets you move around the file using arrows or H, J, K and L. You can delete lines with "dd" or characters with "x". EDIT mode lets you add or change text. From Movement mode, use "i" or "a" or "o" to enter Edit mode in different ways. Read docs for details. Third, COMMAND mode can be reached by hitting ":" from Movement mode. From here, you can issue a variety of commands to save, search, bulk edit, etc.
    • From Movement mode, you can save your file and exit using "ZZ". From edit mode, you can do this with the command "wq" (hence the ":wq" mentioned elsewhere).
  • Alternately, you can set a new crontab by piping information into the crontab command. Note that this will erase any existing crontab you may have. Run this in your shell updating the URL of your script as required:

    echo "0 20 * * * wget http://example.com/path/to/file.php" | crontab -

  • crontab -l will show you what your current crontab contains.

Hope this helps.

UPDATE #2 (per comments below):

$ tmpfile=/tmp/foo.$$
$ crontab -l > $tmpfile
$ echo "30 6 * * * Mail -s wakeup [email protected] <<< 'Time to wake up.'" >> $tmpfile
$ crontab - < $tmpfile && rm $tmpfile
like image 128
ghoti Avatar answered Jan 16 '23 03:01

ghoti


You can try Ctrl+X. This will ask you for changes to save or not before exit. In that just give Shift+Y so you will get out of there. Try it.

like image 26
KinjalB Avatar answered Jan 16 '23 03:01

KinjalB