About Aman Brar

  • Website: or email
  • Biography:

Posts by Aman Brar:

 
0

How to run a PHP script as Cron job from SSH (Linux shell)

on Nov 12, 2010 in Cron, SSH

You can run a PHP script on cron by setting up a crontab

For this, first login to your shell using an SSH client such as PuTTY. And on the command line, type the following command (here # means the shell prompt):
# crontab -e

That will put you in the Vi editor editing your crontab. First press “i” to begin inserting, then type out your cron line following the format below for each line, then press the “escape” key on your keyboard to get out of insert mode, and then type “:wq” and press “enter” to save it.

The form of a crontab file is: minute hour day month weekday command
For example: 42 4 1 * * /usr/bin/php /full/path/to/myscript.php

The first part (those first five figures) is where the schedule is set. Those numbers and asterisks represent units of time. For the example above, this part: 42 4 2 * * can be read as:
42: The 42nd minute
4: Of the 4th hour
2: of the 2nd day
*: of any month
*: on any day of the week

Or, more briefly – the second day of every month at 4:42 am

If you want to run a script at every minute then simply use this: * * * * *

The second part to know is where the PHP interpreter on your server is run from; to find that path use this command: # which php which would return the directory such as this: /usr/bin/php

The third part is the full path on the server where your script lies.

A proper crontab might look like this:
MAILTO=yourname@yourdomain.com
42 4 1 * * /usr/bin/php /full/path/to/myscript.php

If you do not want to receive the cron output via email then use this:
42 4 1 * * /usr/bin/php -q /full/path/to/myscript.php

The -q flag suppresses HTTP header output. As long as your script itself does not send anything to stdout, -q will prevent cron from sending you an email every time the script runs.

To view your crontab file, type:
# crontab -l

To remove your crontab, type:
# crontab -r

If your PHP scripts do not have executable permissions, 644 or -rw-r–r– for example, then as part of the command portion of the cron line, you must specify the php interpreter and pass it the filename of the PHP script (including full path to the script from your home directory) that you want executed. If your PHP scripts do have executable permissions like 755 or -rwxr-xr-x and they have one of the PHP filename extensions, then you do not need to specify the php interpreter in the command portion of your cron line.

When you explicitly specify the php interpreter /usr/bin/php your scripts need not have filenames ending in .php .phtml .php3 .php4. Conversely, if your script filenames do not end in one of those PHP extensions, then you must explicitly use the php interpreter in the command portion of your cron as above.

Note that changes to your crontab may take a few minutes to synchronize with the main cron server.

Tags: , , ,

 
4

How to use MySQL from SSH (Linux shell)

on Nov 12, 2010 in MySQL, SSH

Using MySQL from SSH may seem to be quite tricky if you’ve never done it before – but fear not – below is a list of MySQL commands that you can use to perform the required actions. Start by logging-in using a SSH client, like PuTTY, to access the shell command line. Below when you [...]

 
0

How to convert HTML to PDF using PHP 5

on Feb 18, 2010 in HTML, PDF, PHP

There are many tools on the internet that allow you to convert files of various formats to PDF – but sometimes you need to make such conversions using your code. In this article, we’ll specifically discuss how to convert an HTML file or content to a PDF file using PHP 5. For the task at [...]

 
10

How to develop a website in Hindi

on Feb 17, 2010 in CSS, Fonts, HTML

You can develop a website in Hindi – or any other language – by embedding custom dynamic fonts. Although, embedding custom fonts has always been a painful experience for web developers and designers, but the latest trends in web design brought the idea back to life with new techniques. In this article you’ll find the [...]

 
0

How to include one HTML file inside another

on Feb 12, 2010 in HTML

Including an HTML file inside another HTML file can be achieved using Apache’s SSI (Server Side Include). In this article, we’ll stick to the topic only and for further details on usage and scope of SSI you may refer to this article. Configuring your server to permit SSI To permit SSI on your server, you [...]

bottombar