Tuesday, January 04, 2011

Programatically managing a user's crontab

In Linux, there is a central crontab or scheduler for the entire system. Trying to schedule things through here is often messy and full of problems, luckily there is also a crontab for each user (unless intentionally disabled). Accessing this scheduler is simple, just run crontab -e and it will open in vi, you then edit the lines as you would any other cron.

But what if you want to edit this programatically - i.e. perform the same task on multiple computers? You dont want to log into each computer and make the change manually, but how do you script it?
I found the conversation at http://www.unix.com/shell-programming-scripting/17896-can-we-edit-crontab-using-shell-script.html that was discussing this and has a solution. According to user Just Ice
have your script ...

a. crontab -l > $tmpfile
b. edit $tmpfile
c. crontab $tmpfile
d. rm $tmpfile

System Configuration and Management -- Build a simple RPM that packages a single file

This is an interesting objective - something that I have never had to do before. After looking around at various tutorials and such, the wording of the objective is even more confusing - RPM packages are designed to install programs, not just copy a file. For example, there is a great how-to for creating RPM packages at http://fedoraproject.org/wiki/PackageMaintainers/CreatingPackageHowTo.

A few days looking and I actually came upon a need to create a package - I want to have a silent install of Linux in our datacenter that copies custom scripts to the systems. The best way to handle this would of course be to build an RPM package that included just the one script. A little Googling and bingo! http://lincgeek.org/blog/?p=303 has just the information I needed on how to package a single file and direct it to install into a specific location.

STEP 1: Install the necessary packages
Each site you go to says something different, but it appears that the rpmdevtools contains all you need -- yum install rpmdevtools

STEP 2: Setup the folder structure
mkdir -pv rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp}

STEP 3: GZip the source file
Assuming you have a script named HelloWorld.sh
  1. Move to the rpm/SOURCES folder
  2. Make a temporary directory with a '-1' at the end -- mkdir HelloWorld-1
  3. GZip the source -- tar czvf HelloWorld-1.tar.gz HelloWorld-1/
STEP 4: Create the spec file
This is the hard part - configuring the RPM on what to build, install, and configure. A sample spec file can be created by running rpmdev-newspec SPECS/HelloWorld.spec, but there is still a lot to add and remove to make this work.
Below is a spec file I created using the sample file
Name:           RandomRootPass
Version:        1
Release:        1%{?dist}
Summary:        Random Root Password changer

Group:          Misc
License:        GPL
#URL:            http://localhost
Source0:        RandomRootPass-1.tar.gz
BuildArch:      noarch
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)


#BuildRequires:
#Requires:

%description
Script to reset the root password to a random value

%prep
%setup -q


%build


%install
#rm -rf $RPM_BUILD_ROOT
#make install DESTDIR=$RPM_BUILD_ROOT
install -m 0755 -d $RPM_BUILD_ROOT/opt/RandomRootPass
install -m 0755 RandomRootPass.pl $RPM_BUILD_ROOT/opt/RandomRootPass/RandomRootPass.pl


%clean
#rm -rf $RPM_BUILD_ROOT


%files
%dir /opt/RandomRootPass
/opt/RandomRootPass/RandomRootPass.pl

#%defattr(-,root,root,-)
#%doc



#%changelog


STEP 5: Build the RPM
Once the spec file is complete, build the package with rpmbuild -qa SPECS/HelloWorld.spec. Assuming no errors occurred, your package is under the RPMS folder. Otherwise attempt to decipher the errors and try again.