Script that will look the same as Cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that will look the same as Cron
# 15  
Old 08-09-2019
So download and run your own crond as mentioned .... Smilie

You do not need to rewrite, in a shell script, code available in C to build and / install in your own directory and run yourself.
# 16  
Old 08-09-2019
Hello,

OK, here's a quick hacked-together version of a script that more-or-less does what crond would do. As Neo has mentioned however, this is not the best idea, as you're kind of needlessly re-inventing something that already exists and which will definitely do a far better job than this shell script. If the customer/client/maintainer/whoever actively doesn't want you running scheduled tasks on the server, then they're not going to be happy about you doing it no matter how you go about doing it. Alternatively if you have a hard requirement to run scheduled tasks and the customer just doesn't want you installing software, then you need to have a conversation with your customer explaining that they need to install crond to enable you to do the work that you've been asked to do, otherwise you can't do it. If the job you need to do has a 100% requirement for scheduled tasks, then whoever provides this system either has to install or let you install crond, or they need to accept that the job can't be done since they won't let you have the tools you need to do it.

Anyway - bearing in mind all the above caveats (and the potential no doubt for bugs and issues that could be lurking with this approach), here's a quickly knocked-together script that would do more or less what you need.

Code:
$ cat cron.tab
Fri,16,13,echo "It's thirteen minutes past four !"
Fri,16,15,echo "It's quarter past four !"
$ cat script3.sh
#!/bin/bash

crontab=/home/unixforum/282472/cron.tab

while true
do
        while read cron
        do
                runday=`echo "$cron" | /usr/bin/awk -F, '{print $1}'`
                runhour=`echo "$cron" | /usr/bin/awk -F, '{print $2}'`
                runminute=`echo "$cron" | /usr/bin/awk -F, '{print $3}'`
                runcommand=`echo "$cron" | /usr/bin/awk -F, '{print $4}'`

                nowday=`/usr/bin/date +%a`
                nowhour=`/usr/bin/date +%H`
                nowminute=`/usr/bin/date +%M`

                if [ "$runday" == "$nowday" ] && [ "$runhour" == "$nowhour" ] && [ "$runminute" == "$nowminute" ]
                then
                        /usr/bin/date
                        $runcommand
                fi
        done < "$crontab"

        /usr/bin/sleep 60
done

$ date
Fri Aug  9 16:11:14 BST 2019
$ ./script3.sh
Fri Aug  9 16:13:16 BST 2019
"It's thirteen minutes past four !"
Fri Aug  9 16:15:16 BST 2019
"It's quarter past four !"
^C
$

These 2 Users Gave Thanks to drysdalk For This Post:
# 17  
Old 08-09-2019
Quote:
Originally Posted by drysdalk
Hello,

OK, here's a quick hacked-together version of a script that more-or-less does what crond would do. As Neo has mentioned however, this is not the best idea, as you're kind of needlessly re-inventing something that already exists and which will definitely do a far better job than this shell script. If the customer/client/maintainer/whoever actively doesn't want you running scheduled tasks on the server, then they're not going to be happy about you doing it no matter how you go about doing it. Alternatively if you have a hard requirement to run scheduled tasks and the customer just doesn't want you installing software, then you need to have a conversation with your customer explaining that they need to install crond to enable you to do the work that you've been asked to do, otherwise you can't do it. If the job you need to do has a 100% requirement for scheduled tasks, then whoever provides this system either has to install or let you install crond, or they need to accept that the job can't be done since they won't let you have the tools you need to do it.

Anyway - bearing in mind all the above caveats (and the potential no doubt for bugs and issues that could be lurking with this approach), here's a quickly knocked-together script that would do more or less what you need.

Code:
$ cat cron.tab
Fri,16,13,echo "It's thirteen minutes past four !"
Fri,16,15,echo "It's quarter past four !"
$ cat script3.sh
#!/bin/bash

crontab=/home/unixforum/282472/cron.tab

while true
do
        while read cron
        do
                runday=`echo "$cron" | /usr/bin/awk -F, '{print $1}'`
                runhour=`echo "$cron" | /usr/bin/awk -F, '{print $2}'`
                runminute=`echo "$cron" | /usr/bin/awk -F, '{print $3}'`
                runcommand=`echo "$cron" | /usr/bin/awk -F, '{print $4}'`

                nowday=`/usr/bin/date +%a`
                nowhour=`/usr/bin/date +%H`
                nowminute=`/usr/bin/date +%M`

                if [ "$runday" == "$nowday" ] && [ "$runhour" == "$nowhour" ] && [ "$runminute" == "$nowminute" ]
                then
                        /usr/bin/date
                        $runcommand
                fi
        done < "$crontab"

        /usr/bin/sleep 60
done

$ date
Fri Aug  9 16:11:14 BST 2019
$ ./script3.sh
Fri Aug  9 16:13:16 BST 2019
"It's thirteen minutes past four !"
Fri Aug  9 16:15:16 BST 2019
"It's quarter past four !"
^C
$

thank you very much sir. i will take note of that. from here i will create my own. thx thx..
# 18  
Old 08-13-2019
Probably a daft question, but why can you not use cron? It seems an odd enforcement. If you are writing the code, then you probably have access to do all sorts of nasty stuff anyway. I would not think that a scheduled job is necessarily any more dangerous. The schedules would need to be preserved or shared if you have clustered servers or you migrate to a new server. There may be restrictions that are in place to avoid everybody scheduling loads of things and clogging the machine up and there needs to be some control. Assuming that this job that will do a thing in general rather than for you personally, then maybe a service account would be more suitable (a non-personal account that you/team can sudo to) which could be given access to use cron.

Is there a good reason for the rule? Personally I have not restricted anyone because of one from these:
  • They cannot get to the command line (business users)
  • They know what they are doing (application developers or support staff)
  • If they get it wrong, it's only what they could do anyway.......

It's important that jobs run as a person don't become critical to production running, I agree, but that's a cultural thing too. I've had much experience where a savy person has set something up themselves and then they get their PC replaced and it's all lost or credentials to a database change or are restricted and suddenly their entirely unsupported report stops working. It's worse when they leave and nobody knows wheat was done, what it did or anything except "Bob used to do something each month"


Can you say why you are not permitted to use a scheduler? Anything else is either a bad solution calling at repeatedly (probably also restricted) or requires you to have code running all the time all over the place which is exposed to failure and is unlikely to get restarted on boot until you restart it.



Thanks, in advance,
Robin
# 19  
Old 08-13-2019
Quote:
Originally Posted by meister29
thank you very much sir. i will take note of that. from here i will create my own. thx thx..
Be a little careful how you schedule jobs with this concept, as only one job will run at a time and if another job is due and a long running job hasn't finished yet the other job will not be run. Also if the machine is restarted or you script is killed then the jobs will not be run.

Cron is still your preferred option hear, as all these sort of issues, and others we haven't thought of yet, are dealt with out of the box.
This User Gave Thanks to Chubler_XL For This Post:
# 20  
Old 08-13-2019
Quote:
Originally Posted by rbatte1

Can you say why you are not permitted to use a scheduler? Anything else is either a bad solution calling at repeatedly (probably also restricted) or requires you to have code running all the time all over the place which is exposed to failure and is unlikely to get restarted on boot until you restart it.
My best guest is that it is disguised schoolwork. When people post these types of posts the main reason is that their instructor has told them to "write a script like cron" and then they make up a story about "not having access to cron at work," for example, to get others to help them online.

That's my experience over the past two decades as to why people ask questions with such "constraints" and insist on using a particular solution or approach.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script in cron

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)
Discussion started by: aydj
2 Replies

2. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

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)
Discussion started by: okonita
3 Replies

3. Shell Programming and Scripting

cron a script

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)
Discussion started by: landog
3 Replies

4. Solaris

Script for cron

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)
Discussion started by: frankoko
2 Replies

5. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

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)
Discussion started by: instant000
15 Replies

6. Solaris

Need to cron the script

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)
Discussion started by: jaiankur
3 Replies

7. Shell Programming and Scripting

script not working in CRON

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)
Discussion started by: abhijeetkul
5 Replies

8. AIX

problem with a script and the cron

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)
Discussion started by: pascalbout
1 Replies

9. UNIX for Dummies Questions & Answers

Cron + Script = No Output?

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)
Discussion started by: jmf77
4 Replies

10. Shell Programming and Scripting

Cron Script Q

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)
Discussion started by: doublek321
2 Replies
Login or Register to Ask a Question