Sponsored Content
Top Forums Shell Programming and Scripting Script that will look the same as Cron Post 303037672 by drysdalk on Friday 9th of August 2019 09:29:08 AM
Old 08-09-2019
Hi,

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:

Code:
$ cat script.sh 
#!/bin/bash

runtime=`/usr/bin/date -d '2019-08-09 14:22:00' +%s`

while true
do
        nowtime=`/usr/bin/date +%s`

        if [ "$runtime" == "$nowtime" ]
        then
                date
                echo "It's time to run"
                exit 0
        else
                date
                echo "It's not time to run, I'll keep waiting"
        fi

        /usr/bin/sleep 1
done

$ ./script.sh 
Fri Aug  9 14:21:50 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:51 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:52 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:53 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:54 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:55 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:56 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:57 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:58 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:21:59 BST 2019
It's not time to run, I'll keep waiting
Fri Aug  9 14:22:00 BST 2019
It's time to run
$

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.

Hope this helps !
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
NPM-RUN-SCRIPT(1)                                                                                                                NPM-RUN-SCRIPT(1)

NAME
npm-run-script - Run arbitrary package scripts SYNOPSIS
npm run-script <command> [--silent] [-- <args>...] alias: npm run DESCRIPTION
This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts. run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts. As of ` https://blog.npmjs.org/post/98131109725/npm-2-0-0, you can use custom arguments when executing scripts. The special option -- is used by getopt https://goo.gl/KxMmtG to delimit the end of the options. npm will pass all the arguments after the -- directly to your script: npm run test -- --grep="pattern" The arguments will only be passed to the script specified after npm run and not to any pre or post script. The env script is a special built-in command that can be used to list environment variables that will be available to the script at run- time. If an "env" command is defined in your package, it will take precedence over the built-in. In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For example, if there is a devDependency on tap in your package, you should write: "scripts": {"test": "tap test/*.js"} instead of "scripts": {"test": "node_modules/.bin/tap test/*.js"} to run your tests. The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of ` https://github.com/npm/npm/releases/tag/v5.1.0 you can customize the shell with the script-shell configuration. Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run. npm run sets the NODE environment variable to the node executable with which npm is executed. Also, if the --scripts-prepend-node-path is passed, the directory within which node resides is added to the PATH. If --scripts-prepend-node-path=auto is passed (which has been the default in npm v3), this is only performed when that node executable is not found in the PATH. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install, just in case you've forgotten. You can use the --silent flag to prevent showing npm ERR! output on error. You can use the --if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain. SEE ALSO
o npm help 7 scripts o npm help test o npm help start o npm help restart o npm help stop o npm help 7 config January 2019 NPM-RUN-SCRIPT(1)
All times are GMT -4. The time now is 04:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy