Michael
Michael

Reputation: 22967

Editing crontab remotely with ant sshexec doesn't not work

i'm using the following ant script to remove a crontab on a remote machine:

<target name="remove-crontab">
    <echo message="Removing Crontab" />
    <sshexec host="${host}" username="${username}" password="${password}" command="crontab -r" trust="true" failonerror="false" />
</target>

When I run the script, even if there is a crontab under the username I get no crontab for username

My only guess is the when connected through SSH some privileges are taken. Anyone know what's this about ?

Thanks.

Upvotes: 1

Views: 1296

Answers (1)

chown
chown

Reputation: 52778

Make sure that a crontab file exists for that user in /var/spool/cron/ and check to see if its owner/group and permissions are correct:

[ 09:59 root@hozbox ~ ]# ll  /var/spool/cron/
total 12K
-rw------- 1 jon    root 601 May  2  2011 jon
-rw------- 1 root   root 460 Aug 31  2010 root
-rw------- 1 tomcat root 601 Nov 17  2010 tomcat

A solution to your issue may be to just empty the crontab by directing /dev/null into it instead of using -r. This would ensure that the cron jobs are removed, but the crontab file doesnt get deleted from /var/spool/cron (the -r option just deletes this file, which is why the second time you run crontab -r, it says no crontab for user):

[ 10:40 jon@hozbox ~ ]$ ssh jared@localhost "crontab -r"
no crontab for jared
[ 10:40 jon@hozbox ~ ]$ ssh jared@localhost "crontab < /dev/null"
[ 10:40 jon@hozbox ~ ]$ ssh jared@localhost "crontab -r"
[ 10:40 jon@hozbox ~ ]$ ssh jared@localhost "crontab -r"
no crontab for jared

:

<target name="remove-crontab">
    <echo message="Removing Crontab" />
    <sshexec host="${host}" username="${username}" password="${password}" command="crontab < /dev/null" trust="true" failonerror="false" />
</target>

I'm not sure, but command="crontab < /dev/null" might need to be command="crontab &lt; /dev/null".


usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)
        -s      (selinux context)

Upvotes: 1

Related Questions