If you definitely don't have access to the crontab for the user you want to run the script as, and if you can't get the person who has root on the box in question to set it up for you, then one approach would be to have a script that runs in an infinite loop until a target time arrives, and then runs whatever task it's meant to run at that time.
Here's an example of such a script, showing the code and me starting to run it roughly ten seconds before the appointed time:
So the basic idea is we define our target time in the runtime variable at the top, converting a human-readable date into the UNIX epoch (the number of seconds since the start of 1970). We then in an infinite loop check the current time, compare it to our target time, and do something specific if the two match. If the current time is not the target time, we wait one second, and the loop goes round again until the target time arrives.
Note that this isn't entirely ideal, and you'd need to run this via screen or tmux or something similar to ensure that the script didn't die when your terminal session did. But if you really definitely don't have access to an actual system-level task scheduler, this kind of approach will work in a pinch. You might also want to make the sleep command wait for less than one second, just to be sure you don't ever miss the target second you want your job to actually run, but that's the only other issue that immediately comes to mind.
Using the approach from my script, you would just tweak the runtime variable accordingly. For instance, in the provided example:
the target time is very specifically 14:22:00 on the 9th of August 2019. If you wanted, for instance, to make your code run on 25th December 2020 at midnight, you'd just change this to:
and you'd be all set.
--- Post updated at 03:51 PM ---
Hi,
Alternatively, if you wanted a more generic and less specific solution - e.g. a "run this at 14:50 every Friday" kind of situation, without worrying about an actual specific date in the year or specific second - you could do something along these lines:
In that case, you'd really be into the territory of literally re-inventing crontab. For example, you could define an input file format that consisted of runtimes and their associated commands, and have your script read that file and run the commands when the runtimes arrived - in other words, to do pretty much exactly what the crond daemon does.
I've not got time right now to write a full example unfortunately, but if you defined your input format as something like:
and had your script read this file line-by-line, breaking out the variables in each line into the days, hour, minutes and commands, you could then run those commands via the same kind of infinite-loop approach as you've seen in the examples given thus far.
If you absolutely don't have access to crontab, and can't get the maintainer of the server to put in the cron entries for you, then this would more or less be the only way you'd get a general-purpose task scheduler going. But you'd probably want to be sure that the maintainer of the server was happy with you doing this, since presumably they've blocked access to cron to prevent people running commands on a set schedule, which is what you'd end up doing anyway here.
I have a script that require input from terminal at some point (either a,b or c)
How can I cron the script and automatically assume c as the input. (2 Replies)
Hello gurus,
I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired
report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup
is shown below:
#!/bin/ksh
db2... (3 Replies)
This has to be the ultimate newbie question...
I have a script that works well. To run it, I cut and paste it into a putty session.
Is there a way to put the script into a file and just run that file -- like a DOS batch file?
I'd like to schedule the file to run daily.
Thanks,... (3 Replies)
Dear All,
I have an assignment about collecting /var/adm/messages on each server running Solaris 9. All these servers will be installed with a script that collect all the messages in cron. All the messages will be collected by a single server.
I have a script to collect all the messages, but... (2 Replies)
Good morning. I am searching for "how-to"'s for some particular questions:
1. How to write a script in HP-UX 11.
2. How to schedule a script.
3. How to "call" scripts from the original script.
4. How to su to another user from within a script.
This is the basics of what the... (15 Replies)
I want to cron one script which should run every second Thursday (in general Nth occurence of any day) of every month.
I am not getting the exact idea of doing this; Please guide.
Regards, (3 Replies)
guys
i have written a very simple script .it runs manually well.
but when i put it in cron,it doesn't give the desired output.
script looks like this:
#! /usr/bin/sh
#script for loading data in table using ctl file/Abhijeet K/08.07.2006
/svm_wl1/. .profile
cd... (5 Replies)
hello
I use a script to give me the number of users, at each hour.
this script read a file where there is the number of users for each hour and for each month (so 12 files per year).
If i execute the script (root), it is ok, i have xxx users for each hour.
if I put the script in the cron... (1 Reply)
I've got an *extremely* simple script I want to run every minute:
#!/bin/sh
ping -c 1 192.168.1.20 > ~/onlinestatus.txt
So, the script is called "status", it's executable, and in the correct path, etc. In a terminal window (I'm using Mac OS X), I can type status, and it will create... (4 Replies)
When logged in as root if I type "env" there are a bunch of environment settings including one for CLASSPATH. However, I ran a cron script that ran this command "env > cronEnv". I noticed that the environment variables were entirely different inside the script. There wasn't even a CLASSPATH... (2 Replies)