Archive

Posts Tagged ‘command-line’

Redirect output to file

March 9th, 2009 No comments

Normally when you execute a command in your shell you’ll get the output direct on your screen. But it’s also possible to redirect this output to a file, for example for logging purposes.

Example:

php myscript.php > mylog.log

Now all output from myscript.php will go in the mylog.log file. This is called the “standard output” (stdout). But when a PHP error occurs it will not be written to the mylog.log file. Instead it will be printed on your screen. This is the “error output” (stderr), and to write this to mylog.log you have to use this:

php myscript 2>&1 mylog.log

This will send all output to mylog.log including the errors. It’s also possible to only write the error ouput to a log file.

php myscript 2> mylog.log

Combine this with ‘running processes as background jobs ‘ and you can run your scripts/command in the background but still be able to watch the progress in the log file.

php myscript 2>&1 mylog.log &

And now watch your log with

tail -f mylog.log

You will see new lines being written to the log in real time!

These different outputs are streams, read more info about this here.

Running processes as background jobs

March 2nd, 2009 1 comment

On a linux terminal you can simply run a process in the foreground like it normally does. But if you close your terminal session the process will stop running. If you run a large database import you don’t want to wait for it to finish, so you want to run it in the background.

To do this you can simply put a ‘&‘ behind the command you want to execute, for example:

mysql -u root -p < import.sql &

But it’s also possible to put a process to the background when it’s already running. You can do this with the keyboard shortcut ctrl + z. Then you ‘stop’ the process. You should see a message like this:

[1]+  Stopped                 mysql

Now you can choose two things, one is to run it in the foreground, the other is to run it in the background.
Just type bg <enter> and the process will continue running in the background. With fg <enter> it will run in the foreground.

You can see what processes are running in the back- or foreground with the command jobs.

Commandline tips

February 20th, 2009 No comments

Do you think you know all great commands on the command-line? Then you have to take a look at this site. It’s a overview of the most used and most popular commands for linux.

http://www.commandlinefu.com

Categories: Linux, Websites Tags: , ,