Click to See Complete Forum and Search --> : RE: Running scripts/commands unattended


Carl Pender
04-16-2003, 01:27 PM
I was wondering if there is such a way to run a script with two commands, whereby the first command is executed straight away and then the second command will be executed in 30 minutes or so after the first.

What I have is a wireless network which I want to control the acces the network deciding who is let on and for how long.

I have two scripts, one called addmac, which allows a certain MAC address access the network and another called removemac which stops the MAC address having access to the network.

Is this possible?

Thanking you in advance.

Carl

ev8r
04-16-2003, 01:35 PM
you could put both in a script that looked something like this

addmac
sleep 1800
removemac
exit 0 (or whatever)

the sleep command takes its arguement in seconds

scinerd
04-16-2003, 01:37 PM
how about

#!/bin/sh
addmac &
sleep 1800
removemac

this would run add mac in the background then sleep for 30 minutes and run the other one. If you remove the & then removemac will run 30 minutes after addmac finishes.

Carl Pender
04-16-2003, 01:39 PM
Thanks! Is it possible for that script be called again (say for another user) while it's asleep?

ev8r
04-16-2003, 02:15 PM
putting a '&' after <scriptname> assigns a unique PID to it letting your main script run freely

so your might put it in .bash_profile or whatever as:

macscript&

bwkaz
04-16-2003, 05:50 PM
Originally posted by ev8r
putting a '&' after <scriptname> assigns a unique PID to it letting your main script run freely You don't even need the &. All scripts are reentrant, because they are run in separate processes.

It'll work without the & after the script. You won't be able to run it from the same controlling TTY, but if you start the script on one virtual console (ctrl-alt-f1), then while it's sleeping start another instance on another virtual console (ctrl-alt-f2), it'll still work.

Carl Pender
04-17-2003, 08:46 AM
Thanks guys that worked fine!

Is there any way to make so that I could run the script a number of times. That is, for instance, if one user logs on for 30 minutes, then 5 minutes later another user logs on but he can't log on because the script is asleep.

The virtual console thing works (thanks bwzak) but I need to automate so that there sin't the need for someone to change console (using ctrl-alt-f1). Does anyone have any suggestions?

Thanks again guys, you've been very helpful!

bwkaz
04-17-2003, 09:54 AM
You can run it in the background, as was posted previously, with ./scriptname & or whatever.

Carl Pender
04-17-2003, 10:46 AM
I'll try that thanks!

Carl Pender
04-25-2003, 12:59 PM
Using ./script & doesn't work I'm afraid. What I need to allow someone on the network and then 5 hours later I want to log them off again. If I put the program to sleep I can't log on with another user, while it's asleep.

Would cron be of any use in this situation? I cant seem to get my head around it though.

bwkaz
04-25-2003, 02:21 PM
Cron is useful for recurring tasks -- ones that you need to happen at every expiration of an interval. For example, rotating logs every week is ideal for cron.

To get something to happen once, in 5 hours (for example), is an ideal job for at or batch or something like that. However, I don't know how to use them, because I've never had to.

Maybe worth a shot, anyway...

Stuka
04-25-2003, 04:49 PM
Why not use the at command? at is for one-time commands, so your script could be:addmac
at -f removemac now + 5 hours
That runs your addmac script now, and then the commands in your removemac script in 5 hours.
<edit> Just noticed you sorta pointed this way, bwkaz ;) </edit>

Carl Pender
04-26-2003, 10:13 AM
Thanks guys that works perfectly:D :D

I was just wondering though if it is possible to call a cgi or a web page using this "at" command.

What I have is:

sudo at now + 1 minute -f /usr/local/apache/cgi-bin/goodbye.cgi

which calls another web page which removes the person from accessing the web but instead of actually running displying the web page, it just runs behind the scenes. In my /var/spool/mail/root file the source code is all there. The command works but it doesn't display the page.

Basically I am asking how to forward to the page after a given time.

Thanks again guys!

chrism01
04-27-2003, 08:56 AM
Depends if you have a wraparound cgi for whatever the user is doing. You may be able to setup a timer loop/check inside the prog and have it count down, then display the new page when it times out.
Alternayively, spawn off a separate timer proc and have it write to file when it times out. Have the main prog check the file whenever it can , then re-direct to new pag.
Cron is a detached process ie does not have access to terminals; same with at.
They are designed to run in the background only.
Using the '&' enables you to run in the background but still talk to the terminal you started from.

Good luck.