Archive for Expert

How to find the current working directory of a process…

Ok, this might sound insane, but this is the type of question you get asked sometimes, or the situation where you need to know comes up.

I was once asked what the current working directory was of a process on the server… often times I don’t even think about it, however the script was writing files, and we had no idea where ;)

I tracked it down to a pretty simple method

> ps -awux | grep [process]

root 90 0.0 0.0 1016 228 ?? Is 27Apr05 1:01.81 /usr/sbin/cron

> ls -l /proc/[pid from above, which is 90]/cwd

lrwxrwxrwx 1 root root 0 Nov 4 12:56 /proc/90/cwd -> /var/spool

And it’s current working directory is /var/spool

Neat :)

Comments

SSH Keys for passwordless logins to other servers

SSH Keys are an awsome tool.. and they’ll save your fingers from typing your password 10,000 times a day. They’re also really easy to setup:

login to your main server, or home machine as the user you normally are logged in as.

username@yourserver.com [16:00:51]
[~/.ssh]: /usr/bin/ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_dsa): [ENTER]
Enter passphrase (empty for no passphrase): [ENTER]
Enter same passphrase again: [ENTER]
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:1e:00:fb:d5:57:45:20:c3 username@yourserver.com

username@yourserver.com [16:00:59]
[~/.ssh]: cat id_dsa.pub

ssh-dss [EDITED OUT HUGE TEXT HASH ] username@yourserver.com

Copy the entire output from your server to your clipboard.

Now login to the remote server you want to be able to access without a password and:

$ vi ~/.ssh/authorized_hosts

go to the bottom of the file and insert and paste the line, then [ESC] [ESC] :wq!

and give it a test

$ ssh username@remoteserver.com

Comments

Server Backups, the hardcore way.

I’ve been caught too many times without good backups, so I’ve started to be a bit smarter about things… I try and automate and cron all the backups early on in a servers existance and just have one big backup drive on a server somewhere that keeps everything.

Here’s an example of this is a simple site backup script that I run.

/root/scripts/syncserver


/usr/local/bin/rsync -av –exclude “*_log” -e “ssh” / username@backup.server.com:/path-to-backups/`/bin/hostname`/

This runs rsync updating any new files on the filesystem to our backup server. Now I just create an ssh key for root to the other machine’s username and copy the entire box over… this isn’t a PRETTY restore if I had to use it, but at least all data, config files, new files, users, etc are saved on the other machine.

Modify paths as necessary.

Comments

What is going on… or, why is the server slow when your not around.

I’ve been called in the middle of the night with “the server is slow” being the emphasis of the call. Sometimes it’s not obvious using standard utilties, or sometimes it only happens during certain times.

I’ve used a simple script to eliminate this issue, and run it from cron every minute or 5 minutes to give me output on whats going on with the system.

This is ment for temporary use only, or at least make sure you clear the files every once and a while, because they get pretty big, thus I wrote a cyclecheck script and even a cyclerebootcheck script to see why a machine rebooted.



check script:

#!/bin/sh
echo “——————-” >> /data/`/bin/hostname`.load
echo “——————-” >> /data/`/bin/hostname`.psaux
/bin/date >> /data/`/bin/hostname`.load
/bin/date >> /data/`/bin/hostname`.psaux
/usr/bin/w >> /data/`/bin/hostname`.load
/usr/bin/w >> /data/`/bin/hostname`.psaux
ps -awux >> /data/`/bin/hostname`.psaux



cyclecheck script:
mv /data/`/bin/hostname`.psaux /data/`/bin/hostname`.psauwx.old
mv /data/`/bin/hostname`.load /data/`/bin/hostname`.load.old


cyclerebootcheck script:

mv /data/`/bin/hostname`.psaux /data/`/bin/hostname`.psauwx.reboot
mv /data/`/bin/hostname`.load /data/`/bin/hostname`.load.reboot
echo `hostname` rebooted `date` | mail pageadmin@site.com



install script:

echo “*/1 * * * * root /root/scripts/check” >> /etc/crontab
echo “1 0 * * * root /root/scripts/cyclecheck” >> /etc/crontab
echo “/root/scripts/cyclerebootcheck” /etc/rc.local
killall -HUP crond



I place them all in /root/scripts and chmod -R 755 /root/scripts, then run /root/scripts/install to place these things into the crontab.

Then you can view the output in /data/hostname-of-server.load or /data/hostname-of-server.psaux each with a timestamped entry.

——————-
Wed Nov 2 00:02:00 EDT 2005
12:02AM up 123 days, 23:45, 0 users, load averages: 0.15, 0.28, 0.24
(what users are logged on listed here)
USER TTY FROM LOGIN@ IDLE WHAT
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
root 24498 0.0 0.0 984 224 ?? R 12:02AM 0:00.00 ps -awux

It works at least.. edit for directories you need, this is written for freebsd in this case.

Comments