Categories
Linux shell

Yum install dependencies for a local RPM

Got an RPM laying about you need to install but it has a bunch of dependencies? well let yum do the heavy lifting.

yum --nogpgcheck localinstall SomeApp.noarch.rpm

 

Categories
Linux shell tuts

Remove all old installed but unused kernels

I just noticed I had 8 kernels installed on a machine. dont need but one…. so I rebooted to make sure I was using the newest, and removed all the old ones via these simple commands :

 

Ubuntu / Debian / dpkg / Apt:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Thats a one liner, so just copy paste the whole thing.

Centos / RHEL / Yum / RPM:

uname -r

See which kernel is being used.

rpm -q kernel

get a list of installed kernels

yum install yum-utils

make sure yum-utils is installed

package-cleanup --oldkernels --count=1

Cleanup old packages, the count can be any number and it will keep that number of the latest packages. count 2 will keep the current newest and one more, 3 will keep the current newest and 2 more and so on.

nano yum.conf

edit /etc/yum.conf and look for installonly_limit and change it to any number, this was the kernels will be cleaned up automatically. I have mine set to 3.

installonly_limit=3

 

Categories
Linux shell

Generate file of fixed size via CLI

We recently moved a hosting to a new data center and wanted to test speeds, so I needed fixed size files to see what kind of speeds people could achieve while downloading them. I decided on the arbitrary numbers of 100MB and 500MB. Heres how I generated fixes size files via bash.

for 500 MB :

dd if=/dev/zero of=file.txt count=1000 bs=524288

for 100MB :

dd if=/dev/zero of=file.txt count=10240 bs=10240

Now you can use other things such as /dev/random etc for this. but this is extremely quick. the 500MB file was finished in .47 seconds. That’s less than half a second….

Categories
Linux mysql PHP shell

PHP PDO Mysql 101

PHP PDO MySQL

Needed to make a simple php script with mysql in it, so I decided to use PDO because… well peer pressure 😐 so heres a basic PHP PDO MYsql script.php pdo mysql

The idea is I have a table called table with a field called field in a database called somedatabase. We have 1000 rows of data in said table and we need to pick a random result from the 1000 rows. This is just 1 way of doing it, it isnt the most optimized, neither is it the cleanest. but this will give you a basic idea of how to connect to the DB and get a result. you can expand this of course to do very tricky queries.

 

// Add a user variable for PDO, mysql user
$user='root';
// Add a password for PDO, mysql password
$password='password';
// Setup the mysql connection info
$dsn = 'mysql:dbname=somedatabase;host=127.0.0.1';

// Wrap the actual connection to mysql in try so if it doesn't work you can catch the exception and echo the message.
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

// Pick a number from 1 - 1000 and assign to $rc
$rc = mt_rand(1, 1000);
// Assign the SQL query selecting the data from our DB / field to variable $sql
$sql = "SELECT field FROM table LIMIT 1 OFFSET $rc";
// run the query and for each one assign to variable $field
foreach ($dbh->query($sql) as $row) {
$field = $row['field'];
}

// clear the variable $dbh and return used memory back to usable pool
$dbh = null;

I want to write more articles concerning the trio of PHP PDO Mysql but I don’t do much with PHP anymore so the chance that I have need of the three at this point is slim.

Categories
Linux shell

secure copy ( SCP ) howto 102

Secure Copy (SCP) provides a secure and authenticated method for copying files between hosts. SCP relies on Secure Shell (SSH).

Copy the file “somefile.txt” from a remote host to the local host

scp [email protected]:somefile.txt /some/local/directory

Copy the file “somefile.txt” from the local host to a remote host

scp somefile.txt [email protected]:/some/remote/directory

Copy the directory “somedir” from the local host to a remote host’s directory “anotherdir”

scp -r somedir [email protected]:/some/remote/directory/anotherdir

Copy the file “somefile.txt” from remote host “example1.com” to remote host “example2.com”

scp [email protected]:/some/remote/directory/foobar.txt \

[email protected]:/some/remote/directory/

Copying the files “somefile.txt” and “anotherfile.txt” from the local host to your home directory on the remote host

scp somefile.txt anotherfile.txt [email protected]:~

Copy the file “somefile.txt” from the local host to a remote host using port 1234

scp -P 1234 somefile.txt [email protected]:/some/remote/directory

Copy multiple files from the remote host to your current directory on the local host

scp [email protected]:/some/remote/directory/\{1,2,3\} .

scp [email protected]:~/\{somefile1.txt,somefile2.txt\} .
Categories
Linux shell

Screen start issues

Screen is a tool that allows users to resume a session after they have disconnected. Screen prevents a session from “timing out” or disconnecting SSH connections or local terminal emulators. A single Screen session has the ability to host multiple sessions or “windows.”

I just spun up a new ubuntu 12.04 VPS and installed some basics, I needed to use the screen utility but for some reason i was having issues with screen starting up. well it seems screen didn’t create the socket storage directory and kept giving me this error :

Cannot make directory ‘/var/run/screen’: Permission denied

It’s a very simple fix :

  • sudo mkdir /var/run/screen
  • sudo chmod 775 /var/run/screen
  • sudo chgrp utmp /var/run/screen