LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2015, 10:55 AM   #1
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Rep: Reputation: Disabled
automatically launch bash scripts that are currently user controlled?


I have written some scripts to collect system statistics through vmstat, iostat and sar and another script that controls processes. The process control one reads information from a text file and outputs the data according to the menu option chosen. This is for a project. I have now been told that apparently all of these processes and data gathering have to run automatically and therefore need to be overhauled. I was under the impression that actual memory could only be assigned to a process through the kernel and not the command line. I am using Ubuntu 12.04 server(no GUI) edition.

How, if at all, can I modify the script so that the processes run automatically for scheduled times on startup? This is the system stats collection script(currently controlled by the user)
Code:
#!/bin/bash
#
# Script to get sar statistics#
## sar_stats.sh ##
echo "Enter the number of intervals (seconds) for the sar statistics:"
read int
echo "enter the number of output lines (count) to process:"
read cnt
clear
#set while

    while true; do
    clear
echo "*******************************"
echo "* Choose from the following: *"
echo "*******************************"
echo "* [1] To view process creation statistics *"
echo "* [2] To view I/O and transfer rate statistics *"
echo "* [3] To view paging statistics *"
echo "Press A to quit."
echo "************************"
echo -n "Enter your menu choice [0-15]: "
D=`/bin/date '+%B.%d'`
INT=$int
CNT=$cnt
read mychoice
case $mychoice in
1) echo "process creation statistics";
vmstat $INT $CNT |tee /tmp/sar_proc_stat_$D
echo "This file will be saved to /tmp/sar_pc_stat_$D"
sleep 3 ;;
2) echo "I/O and transfer rate statistics"
iostat $INT $CNT |tee /tmp/sar_IO_TR_stat_$D
echo "This file will be saved to /tmp/sar_IO_TR_stat_$D"
sleep 3 ;;
3) echo "paging statistics"
sar  $INT $CNT |tee /tmp/sar_pag_stat_$D
echo "This file will be saved to /tmp/sar_pag_stat_$D"
sleep 3 ;;
A) #Quit option
    echo You have now quit. Please return to the main menu. 
    exit;;

    *) #Wildcard option
     echo Invalid choice, please make another choice;;

esac
done
 
Old 03-05-2015, 11:42 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
What's to stop you using cron for the job?
 
Old 03-05-2015, 01:36 PM   #3
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
Would cron do it ok? Can it make a script run on startup? or two scripts at the same time?
I am trying to install crontab but it can't be found. I have tried apt-get update but it still can't be found. How can I get it on Ubuntu 12.04 server and desktop versions?
 
Old 03-05-2015, 02:17 PM   #4
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
I have managed to get standard cron installed, rather than crontab. Will that do all of the same things?
 
Old 03-05-2015, 02:33 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
cron is the name of the package itself, crontab is what you use to control the commands and scheduling that will be run by cron. It's provided by the cron package, which AFAIK comes pre-installed on all major distros.

Yes you can run multiple commands at a time, or on startup, or whatever you like. Use "@reboot" to run something on startup, otherwise use the five columns to set a run time in the standard crontab syntax:
http://www.adminschoice.com/crontab-quick-reference

Last edited by suicidaleggroll; 03-05-2015 at 02:35 PM.
 
Old 03-05-2015, 02:35 PM   #6
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
ok thanks. with the above script which I pasted, is it possible to get cron to run all three options(iostat, vmstat and sar) in the background every few minutes? or does a new script need written for each one?

If I have created processes myself, can they be scheduled? Is it possible to use cron to reassign priority and virtual memory size to the processes? and to assign virtual memory to the script so that if memory is low, the lower priority processes will wait?

Last edited by rmcateer; 03-05-2015 at 02:39 PM. Reason: different question
 
Old 03-05-2015, 02:47 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Your script should be modified so it fits the traditional *nix format of
Code:
command [arguments]
rather than an interactive menu system in an infinite loop. That doesn't mean it can't work as-is, but it's really not optimal.

You could boil that entire prompt/read menu system down to:
Code:
int=$1
cnt=$2
mychoice=$3
and go on your way. You could even leave the menu in-place if you want, but just add command line switches so if the user (or script, or crontab) knows what it wants to run it doesn't have to jump through hoops flipping through menus to do it.

As for prioritization, no. cron runs commands at scheduled times (like Windows' Scheduled Tasks), it's not a resource scheduler. You might want to look into something like Maui/Moab and TORQUE.

Last edited by suicidaleggroll; 03-05-2015 at 02:50 PM.
 
Old 03-05-2015, 03:27 PM   #8
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
Well I think that Maui is desktop based whereas I need to use the server only, i.e. No GUI allowed.
My basic objective is written as this: "this project will involve installing the selected server Operating Systems in a selected virtual environment, their configuration in a manner relevant for this domain, generation of data streams to feed into the Operating System and represent the way in which activity will take place in this domain, initiation of processes to monitor and manage collected data, and collection of a system resource footprint for each server."

I have already stressed and gathered statistics on the servers, similar to how they are gathered above. I also have a script which contains a menu with several options. the options allow the user to control processes by pressing an option which will start/stop process, give it a priority, give it a value and a memory and check all of those values. what the text in quotes though suggests is that it is all done automatically, which is my tutor's suggestion. She is asking for an algorithm that controls the files automatically and gathers the data. I already have the statistics and process values written to text files.

Is cron the best way to manage this data then? I thought it could use nice/renice to set priority?
 
Old 03-05-2015, 03:28 PM   #9
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
I have a dialog menu which controls other options such as user passwords. Can cron run and not interfere with this or will the two conflict?
the processes are all intended to be recurring.

Last edited by rmcateer; 03-05-2015 at 03:42 PM.
 
Old 03-05-2015, 04:05 PM   #10
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
If your dialog menu runs when a user logs in, make sure it only runs for interactive sessions.
 
Old 03-05-2015, 04:05 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
All cron does is run a command at a specified time with no user input, either required or allowed. You can not use cron to execute a command that requires user input. Interactive menus are a big fat no, either pull it out or add code to let you bypass it. Interactive menus are terrible anyway...they're decent learning exercises, but an absolute abomination to use. Don't make it a requirement or anybody who actually tries to use this system will curse your name.

nice/renice are the crudest possible way to approach resource scheduling. You could go that route, but you will be EXTREMELY limited in capability.

Just because it's a headless server doesn't mean you can't run GUIs, that's what X forwarding is for. Either way, I highly doubt Maui requires you to run a GUI. I have yet to find any Linux application where that's a requirement, beyond the obvious (word processors, web browsers, etc.).

---------- Post added 03-05-15 at 03:06 PM ----------

Quote:
Originally Posted by rmcateer View Post
I have a dialog menu which controls other options such as user passwords. Can cron run and not interfere with this or will the two conflict?
the processes are all intended to be recurring.
I don't understand what you're asking. Why would they conflict? Please describe your question in more detail. Chances are cron was already installed and already running other scheduled tasks before you even touched this system. The only thing you'll be doing is adding more scheduled tasks to the list.

Last edited by suicidaleggroll; 03-05-2015 at 04:07 PM.
 
Old 03-05-2015, 05:23 PM   #12
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
if i already have a script though that outputs all of the process functionality through the user selecting options, then how hard is it to make it so the processes run automatically? Would I need to overhaul the whole code? is there no simple option such as put the processes in a scheduling folder?

She is asking for an algorithm to run them automatically but i don't think she really understands what that means. if they are scheduled to run automatically in cron, then does that not suffice?

What would an example of an algorithm be for processes running automatically?
 
Old 03-05-2015, 05:56 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
If it's run automatically in the background by cron, who is going to navigate through the menus to get the script to do what you want?

You'd have to write a command file and pipe it into the script as stdin to feed the answers to the menus automatically...which is slow and ugly. Interactive menus are an abomination anyway. Unless they're absolutely required, you should replace all of those prompts and reads with simple command line arguments and a help page. And even if the menu is absolutely required for some reason, you should at a MINIMUM allow the user to pass in the answers as command line arguments to bypass the menu.

This isn't an "overhaul of the whole code", this is a 5 minute adjustment to turn the script into something that's actually usable and able to be automated. You probably could have made those changes in less time than it took me to write this post.

When you write a script for automation, be it by cron or any other means, the first step is to remove all human interaction. All input needs to be grabbed from the command line, and all output should go to log files. Nothing on stdin, nothing on stdout.
 
Old 03-05-2015, 06:25 PM   #14
rmcateer
Member
 
Registered: Oct 2014
Posts: 41

Original Poster
Rep: Reputation: Disabled
well how can view the results of the files without the menu? what is the syntax?
 
Old 03-06-2015, 09:36 AM   #15
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
It's already written to dump the output of the commands to files, so just open them with any text editor or cat.
 
  


Reply

Tags
bash, control, process, ubuntu 12.04



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash; automatically run user script after startx has completed porphyry5 Programming 5 03-05-2013 12:55 PM
LXer: The Launch of the Document Foundation and the Oxymoron of Corporate Controlled "Community" Pro LXer Syndicated Linux News 0 09-28-2010 02:21 PM
Bash Scripting handling user input sections automatically Gavin Harper Programming 3 06-18-2010 10:35 PM
Bash script to launch a terminal window on another user's desktop Excalibre Linux - Software 7 09-29-2008 02:01 AM
How to emulate user on terminal (without bash scripts) Laedrus Linux - Server 2 08-20-2008 01:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration