Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Friday, February 12, 2010

Convert: missing an image filename, Command not found

convert: missing an image filename 'image-test.jpg'.

sh: line 1: image-test.jpg: command not found

Running the convert function through system() in a PHP script may result in the following error, "convert: missing an image filename"

The filename has been passed correctly to convert. The error persists.

Why does the error appear?

Line breaks. During the dump process it is possible that extra line breaks were added to the filenames.

Line breaks are invisible. That is why you cannot see the problem in the filename. The problem is not visible.

The solution is to run a trim() on all the filenames. That will instantly clean up and remove all the extra line breaks.

Monday, October 01, 2007

Bad ; sign errors in crontab file, can't install

Editing crontab with PICO editor might display the error:

"bad ; sign errors in crontab file, can't install"

The ; character is used to combine and run multiple commands in one statement. Your cron job might look like:

# Command One; Command Two
*/3 * * * * cd /usr/home/; php -q whatever.php

The above statement consists of two commands.
The second command would be executed even if the first were to fail.

The solution is to get rid of the ; character. Use the && character instead.

The && character will _not_ execute command two if command one were to fail.

To get rid of the error, edit your cronjob like this:

*/3 * * * * cd /usr/home/ && php -q whatever.php

Saturday, September 29, 2007

Unzip: cannot find or open file.zip

When extracting the contents of a corrupted .zip file, unzip will display the error:

"unzip: cannot find or open file.zip, file.zip.zip or file.zip.ZIP."

Before you move the .zip, be sure to run a test on the .zip:

#unzip -t file.zip

If the test fails, recreate the archive with tar or gzip.

Tuesday, July 17, 2007

Tar: Cannot write: Disk quota exceeded

Tar: Cannot write: Disk quota exceeded

When creating a tar archive, tar will display the annoying error:

"
sdaX: write failed, user block limit reached
tar: Cannot write: Disk quota exceeded
tar: Error is not recoverable: exiting now
"

To resolve this issue, check that:

1) You have two times the free disk space proportionate to the size of the archive you are creating
2) The server does not have restrictions on individual file sizes
3) Hsphere - Double the disk quota value by navigating to the FTP user area and look for the quota parameter.

Note: Why hsphere has two config areas (FTP and Disk Limits) for controlling disk quotas is beyond my understanding.

The FTP quota is the real disk quota that controls free space available on the filesystem.

Monday, May 21, 2007

sudo vs su -

The "sudo" command allows users specified in a sudoers file which is usually located in the /etc directory to perform certain functions (again, as permitted by the sudoers file) that are normally reserved for the root user. The syntax would be something like:

sudo command

where command is normally limited to the root user. You may be prompted for your normal user password, and if the root user has given you permission (in the sudoers file) to perform that action, you can.

The "su" command is a "switch user" command. In its simplest form, typing "su" will prompt you for the root password and if given correctly you get root privileges. Typing "su -" and giving the correct password gives you root's privileges and environment. The "su" command can also be used to gain access to another "normal" user's account if you have that user's password. To do that you would type "su " where is a valid normal user on that system.

Monday, April 02, 2007

Send Attachments with Mutt

Use mutt to send an email through shell.
-a flag to attach an attachment
-s flag is the subject
echo -e creates the message in the body

#!/bin/bash
echo -e "This is the body message of the email" | mutt -a attachment.zip foo@recipient.com -s "This is the email subject"

Sunday, March 04, 2007

Shell Environment Variables

Shell Environment Variables

List the environment vars from a shell prompt:

-sh-3.00$ env

Tuesday, January 23, 2007

Linux Shell

Linux Shell

List Files and Directories without Color Coding

$ls --color=none -all


List Files with Auto-Refresh

while [ 1 ]; do clear; ls -allh; sleep 5; done


Generate and Check MD5

$md5sum FILE > md5sum.txt
$md5sum -c md5.txt

Generate the Date of the Form: January_01_YYYY.$ext

$NOW=`date +%B_%d_%a_%Y


Get the Total Size of a Directory and File

$du -hcs dir/dir
$du -h file.tar.gz


Search for Text Pattern in Files

$egrep "stuff.php" *

Split and Join Files

Split and Join Files

$split -b 450m file.tar.gz

With Prefix
$split -b 450m file.tar.gz PREFIX

Join Files
$cat xaa xab xac >> file.tar.gz

Split and Join Files

Tuesday, January 16, 2007

Restore a MySQL Database

Restore a MySQL Database

Through Shell:

$ mysql -uUserName -pPassword -hlocalhost DatabaseName < mysql_db.sql


Restore a MySQL Database

Backup a MySQL Database

Backup a MySQL Database

Through Shell:

Backup an entire database

$mysqldump -udatabase_user -p --host="host.com" --opt -f database_name > database_backup.sql

Backup through Windows:

c:\> mysqldump -udatabase_user -p --host="host.com" --skip-lock-tables database_name > database_backup.sql


Backup a table

$mysqldump -udatabase_user -p --host="host.com" --opt -f database_name table_name > database_table_backup.sql

-p prompts for a password after the above command is executed

--force, -f

Continue even if an SQL error occurs during a table dump.

One use for this option is to cause mysqldump to continue executing even when it encounters a view that has become invalid because the defintion refers to a table that has been dropped. Without --force, mysqldump exits with an error message. With --force, mysqldump prints the error message, but it also writes a SQL comment containing the view definition to the dump output and continues executing.

--opt

This option is shorthand; it is the same as specifying --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.

The --opt option is enabled by default. Use --skip-opt to disable it. See the discussion at the beginning of this section for information about selectively enabling or disabling certain of the options affected by --opt.

Backup a MySQL Database

Saturday, January 06, 2007

Find and Replace a Matching String of Text

Find and Replace a Matching String of Text

This script will find all .html files (in the current directory and all subdirectories) and replace the string of text 123 with 456

#!/bin/bsh

for file in $(find . -type f -name '*.html')

do

cat $file |sed "s|123|456|g" > $file.new

mv -v $file.new $file

done

Prefix and escape double quotes with a backslash, (double quotes are used by SED)

#!/bin/bsh

for file in $(find . -type f -name '*.php')

do
cat $file |sed "s|include \"file.php\";|require_once (\'/path/file.php\');|g" > $file.new

mv -v $file.new $file
done


Find and Replace a Matching String of Text

Archive and Compress a Directory

Archive, ZIP/Unzip and Compress/Decompress a Directory

$ tar -zcvf archive.tar.gz sourcedirectory/

Exclude a directory(s) from the archive:

$ tar -zcvf archive.tar.gz sourcedirectory/ --exclude=exclude01-dir/* --exclude=exclude02-dir/subdir/*


ZIP

$ zip db.zip foo-file

$ zip -r dir.zip directory/

Extract contents of a GZ archive
$ gunzip archive.sql.gz

or

$ gzip -d archive.sql.gz


Extract contents of a TAR GZ Archive
$ tar -zxvf archive.tar.gz

Recursively Copy a Directory

Recursively Copy a Directory

cp -r source newdestination

Note: newdestination does not need to exist

Recursively Copy a Directory

Monday, October 16, 2006

CRON line 1: Unexpected EOF while looking for matching ``'

CRON line 1: Unexpected EOF while looking for matching ``'

When running a CRON job that uses the command:
NOW=`date +%B_%d_%a_%Y`

the script will abort execution and display the following error:

/bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 2: syntax error: unexpected end of file
To prevent this issue from occurring, dump the contents of the script into a .sh file and run the .sh from CRON.

Sometimes, CRON is set to use sh as opposed to the bash shell that the script needs.

The CRON shell can be modified in the /etc/crontab config file. Requires r00t privileges. Ensure that the CRON daemon is restarted after the modifications.

/sbin/service crond restart

Sunday, October 08, 2006

Delete All Email Using MUTT, Through SSH

Delete All Email Using MUTT, Through SSH

Since I login through SSH frequently, I use the MUTT email client to check email generated through automated scripts, logs etc. There were 40,000 emails that needed to be deleted quickly. I've been unable to find a "select all" option.

The solution involves, pressing the SHIFT and t key simultaneously. This will bring up the "tag messages matching feature". Enter the match that is common with all the emails. Eg: / and hit enter. (this will flag all the matching email with a *)

shift + t
Tag messages matching: /
;d
$

OR

q
yes

The $ key will sync mutt - deleted emails will be purged, mailbox updated etc.

Disable CRON Output From Flooding Email

Disable CRON Output From Flooding Email

When a script is executed through CRON, the output can quickly flood an inbox. To disable output from specific scripts, append the following at the end of the script:

# Disable output completely

*/2 * * * * /usr/local/bin/php /test/foobar.php > /dev/null 2>&1

This script runs every two minutes.
Standard output (1) is redirected to /dev/null
Standard error (2) is directed to the same as standard output (1)

# Redirect script output to a log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt

# Redirect script output to a log file, append log
*/2 * * * * /usr/local/bin/php /test/foobar.php >> /localpath/log.txt

# Redirect script output and CLI error output to the same log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt 2>&1

Note:
/localpath/log.txt - This is standard output (1)
The 2 in the 2>&1 will direct standard error (2) to standard output. In this case, log.txt

# Redirect script output and CLI error output to a different log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt 2>/localpath/cli-errors.txt

# Redirect script output and CLI error output to a different log file, append log
*/2 * * * * /usr/local/bin/php /test/foobar.php >> /localpath/log.txt 2>>/localpath/cli-errors.txt

To test:
Execute this command
ls . IS >> errors.txt 2>>errors.txt

The second alternative is to modify the MAILTO= option and set it to "". This will however, disable all output from being sent to the email address specified in the MAILTO feature.

Another option would be to redirect the output to a text file and call the text file through the browser for analysis.

Note:
The php -q flag suppresses HTTP header output.