schedule automate shell script running not as ROOT

175 Views Asked by At

I have a shell script that I want to run automatically every day at 08 AM, and I am not authorised to use the crontab because I don't have root permission

My home directory is /home/user1/.

Any suggestions?

4

There are 4 best solutions below

0
On BEST ANSWER

Ideally you should have your system administrator add your user account to /etc/cron.allow - without that you do not have permission to use the crontab command, as you probably discovered.

If that is not possible, then you could use a wrapper shell script along these lines (note: untested):

#!/bin/bash

TIME="tomorrow 8am"

while :; do
    # Get the current time in seconds since the Epoch
    CURRENT=$(date +%s)
    # Get the next start time in seconds
    NEXT=$(date +%s -d "$TIME")

    # Sleep for the intervening time
    sleep $((NEXT-CURRENT))

    # Execute the command to repeat
    /home/user1/mycommand.sh
done

You start the wrapper script in the background, or e.g. in a screen session, and as long as it's active it will regularly execute your script. Keep in mind that:

  • Much like cron there is no real accuracy w.r.t. the start time. If you care about timing to the second, then this is not the way to go.
  • If the script is interrupted for whatever reason, such as a server reboot, you will have to somehow restart. Normally I'd suggest an @reboot entry in crontab, but that seems to not be an option for you.
  • If there is some sort of process-cleaning mechanism that kills long-term user processed you are probably out of luck.
  • Your system administrator may have simply neglected to allow users access to cron - or it may have been an explicit decision. In the second case they might not take too well to you leaving a couple of processes overnight in order to bypass that restriction.
0
On

Even if you dont have root permission you can set cron job. Chcek these 2 commands as user1, if you can modify it or its throwing any error.

crontab -l

If you can see then try this as well:

crontab -e

If you can open and edit, then you can run that script with cron.

by adding this line:

* 08 * * * /path/to/your/script

1
On

I don't think root permission is required to create a cron job. Editing a cronjob that's not owned by you - there's where you'd need root.

0
On

In a pinch, you can use at(1). Make sure the program you run reschedules the at job. Warning: this goes to heck if the machine is down for any length of time.