Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

« Newer Snippets
Older Snippets »
19 total  XML / RSS feed 

running scheduled programs in linux

use crontab

Edit the current configuration for crontab with:
crontab -e


Normally we'd created bash script files located in /usr/local/bin and have them specified for execution within our crontab configuration.

grep - search for a string within files within a directory (and sub directories)

// description of your code here

-r gives you recursion
"redeem reward" is the string being searched for
/home/tom is the directory to start the search from

grep -r "redeem reward" /home/tom

postfix - flush queues / logs

Using the program: postsuper

There are three queues (active, deferred, incoming) and four log directories (bounce, defer, trace, flush).

To remove all mails in the deferred queue:

postsuper -d ALL deferred


Note that all of the queues and log dirs are located in:
/var/spool/postfix

reboot or shutdown linux server

how to shutdown a linux box with -f skip fsck file system check, -r reboot.

You must always specify a time or the word: now. Give time in minutes by using: +m

shutdown -rf +1 "Scheduled test of system reboot"


Will reboot the server in one minute, skipping fsck on startup.

send email with attachment from command line

useful for sending db backups

echo command is for the body
-s specifies the subject
-a gives the attachment
finally the recipient
email will be sent as root@serveraxis
echo "database backup for 070614" | mutt -s "backup: myerasmus_db_070614" -a erasmus_db_070614.sql.bz2.gpg [email address]

encrypt files using gpg

encrypt for recipient [Bob] [file]

gpg -e -r Bob file


decrypt, passphrase will be asked for
gpg --output <filename> -d [file] 

linux ubuntu ssh login

To replace the normal text password login with ssh public/private key pair....

Generate a pair at home (creates RSA based key pair)
ssh-keygen -t rsa

Enter in a passphrase when it asks.

upload the .pub key to the server
sftp root@<server address>
lcd /home/<your login>/.ssh
put id_rsa.pub


Login to the server normally as root.
Goto the .ssh directory
Append the public key to the authorized_keys
ssh root@<server address>
cd .ssh
cat id_rsa.pub >> authorized_keys


Then check that /etc/ssh/sshd_config has...
RSAAuthentication yes
PubkeyAuthentication yes

... in it

Restart the ssh server
/etc/init.d/ssh reload


Logout.
Try logging in again as root and you should be prompted for passphrase to your ssh key.

Use
ssh -v root@<server address>

to diagnose problems

linux server runlevel adminstration

To change system services from booting, or to set them to start at boot time use

First step is to create a symbolic link within /etc/init.d to the proggie that you want to service-ize

Here's an example with Apache 2.2
sudo ln -s /usr/local/sbin/apachectl /etc/init.d/apache2
sudo chmod +x /etc/init.d/apache2


Now we have to set which runlevels will use this program

sysv-rc-conf


This is a GUI interface into the various runlevels, what's currently running and not. Use space bar to
toggle whether a service is on or not.

NB: most servers run at runlevel:2

check which runlevel is default for the server by looking at:
/etc/inittab

postfix reload

To restart postfix and read main.cf and master.cf files from /etc/postfix

postfix reload

ubuntu linux headers install

for various reasons (like installing vmware tools)

apt-cache search linux-headers-$(uname -r)
apt-get install linux-headers-$(uname -r)

install rubygems on ubuntu

When compiling and installing rubygems it required the zlib module that wasn't installed in Ruby by default. To add in that functionality, go to the source Ruby directory, the ext directory, the zlib directory, then create the make file, then install it.

Make sure you have the proper library files before doing this (i.e. on Ubuntu its zlib1g-dev, installed via apt-get)

apt-get zlib1g-dev
cd /usr/local/src/ruby-1.8.6/ext/zlib
ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib
make
make install


The 'make' command should have all output lines NOT '...no'
If it does, then zlib1g-dev is NOT installed. Get it via the packages and redo this to create the makefile

After this return to the rubygems source directory and run:
ruby setup.rb

ubuntu search apt-get packages

look up package names and contents via shell

apt-cache search <search string>

vi - change and save settings to the editor

within home directory, edit the .exrc file with set commands

the following sets numbered lines and auto indent.
Full listing of set commands available within by using :set all in vi
set nu
set ai

ubuntu apt-get list installed packages and removal

// description of your code here

will give you a nice list of installed packages...
apt-get install apt-show-versions

apt-show-versions | sort


best way to remove all traces of them is
apt-get --purge remove packagename

(I got fancy and did apt-get --purge remove php5* which removed all of em)

filesharing between linux and windows

In Redhat EL WS 4 (which has Samba pre-installed and running)

become superuser then create a device to mount to
mkdir /mount/<linx_share>


Add an entry to the /etc/hosts for the windows box, with its IP and a hostname
192.168.80.1 DL1


Then create the share in the windows box (in Windows, using normal file sharing.
We'll call it win_share

Then mount the device in Linux
mount -t smbfs ///win_share /mnt/linx_share


eg.
mount -t smbfs //DL1/sshare /mnt/share


Linux should ask for a password (windows password). This assumes that the username between linux and the windows boxes are the same!

After that, you should be able to view the win_share directly from linux and have access to all its files.

recursively find (and delete) files and directories in linux (and svn)

This example looks in the current directory for directories named .svn

That isn't the best way to do it. It will break if you have lots of .svn directories. Then the command line will be too long and you will get 'argument list too long'. Recent bash have enormous buffers so this is less of a problem. xargs solves this problem.

The other problem is any file with spaces in the name will cause bash to treat it as two arguments. Which could give an error or could delete the wrong file. find -print0 and xargs -0 solve this problem.

find . -type d -name .svn

find . -name .svn -print0 | xargs -0 rm -rf



OR make an alias to an egrep command and go nuts

alias g='egrep --color=auto -i -r'


from here on out, just type g "" *

case insensitive
recursive
colored results

using tar to archive files



tar -czf <tar file> <file to archive>[...]

Clear a log file

Run at prompt, replacing development.log with the name of the log file

echo -n > development.log

Change keyboard mapping

In order to get greater than and less than signs without using the retarded French keyboard layout for those keys, remap keys using xmodmap. For help and testing xev can show you what is being typed.

Place the following inside ~/.bashrc to have it run whenever you login.

xmodmap -e "keysym a = a A ae AE less less"
xmodmap -e "keysym z = z Z guillemotleft less greater greater"
xmodmap -e "keysym semicolon = period semicolon horizconnector multiply horizconnector multiply"
xmodmap -e "keysym ugrave = percent percent percent percent percent percent"


The order of the letters after the = sign signify the modifiers to the keypress. The first is without modifier (natural key press of 'a'. Second is with Shift. Third and fourth is after a Mode_Switch (in RedHat Linux that is pressing Shift_R + Alt_R simultaneous, in that order) but without modifier after switching modes. And the last two is with Alt_R and Alt_R + Shift_R modifiers.


To see what currently is mapped use
xmodmap -pke | less
« Newer Snippets
Older Snippets »
19 total  XML / RSS feed