0% found this document useful (0 votes)
7 views10 pages

LP Project Ss

Uploaded by

21311a0572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

LP Project Ss

Uploaded by

21311a0572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Group Project Report

on
LINUX TASK SHCEDULER SCRIPT
Submitted in partial fulfillment of the requirement
For
The award of degree of

BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE & ENGINEERING
2021-2025

Submitted by
B.NIKHITHA 21311A0580
R.UDAY 21861A0540
K.NAZMEEN 21311A0572
CH.REKHA 22315A0507
B. DEEKSHITHA 22315A0510

Under the Guidance of

Mr. Dr. Dheeraj Sundaragiri


Assistant Professor

Department of Computer Science & Engineering


SREENIDHI INSTITUTE OF SCIENCE AND
TECHNOLOGY
(AUTONOMOUS)
Yamnampet (V), Ghatkesar (M), Hyderabad – 501301, T.S.
2021-2025
Project Description: Linux Task Scheduler Script

Overview: The Linux Task Scheduler Script is a Bash-based application that allows
users to schedule, view, and manage tasks on a Linux system. Tasks can be scheduled
to run at specified intervals (in seconds), providing a lightweight and user-friendly
alternative to traditional schedulers like Cron.
The system stores scheduled tasks in a file and executes them in real-time, automating
repetitive processes such as system monitoring, backups, or script execution.

Key Features:

1.Menu-Based Interface
 Provides an interactive menu with options to:
o Add a new task.
o View all scheduled tasks.
o Remove a task.
o Exit the script.
 Enhances user experience by simplifying task management without requiring
manual file edits.

2. Task Scheduling
 Allows users to schedule commands or scripts to run at specific intervals (in
seconds).
 Tasks are stored in a file (tasks.txt) for persistence and easy modification.
3. Task Management
 View Tasks: Displays all scheduled tasks with their frequency and command
details.
 Remove Tasks: Enables users to delete tasks by selecting their task number.

4. Real-Time Execution
 Reads tasks from the file and executes them at their specified frequencies in an
infinite loop.

—1—
 Supports running multiple tasks simultaneously in the background.
5. Error Handling:
Validates frequency input to ensure it's a positive number.
Validates task numbers for operations like remove and stop/resume.
6. Task Status:
Added active and inactive status for tasks.
7. Logging:
Logs actions (add, remove, stop/resume) and task execution details to a log file.
8. Task Pause/Resume:
Allows pausing and resuming specific tasks dynamically.

—2—
Technical Details:

 Bash Scripting: Implements core functionality using shell scripting in a


Linux environment.
 File Management: Stores and retrieves tasks from a text file (tasks.txt).
 Process Handling: Runs tasks in the background using Bash commands.
 Interactive Features: Uses read, echo, and case constructs for a smooth user
experience.

User Experience:

The application is designed to be user-friendly, with an intuitive menu-driven


interface that simplifies the task scheduling process. Users can easily add tasks,
monitor execution, and remove tasks as needed, ensuring a seamless experience for
both novice and advanced Linux users.

Use Cases:

 System Administrators: Automate repetitive tasks like log cleanup, system


monitoring, or backups.
 Developers: Schedule testing scripts or build processes.
 Educators: Teach Linux task scheduling concepts in an interactive and
simplified manner.
.

—3—
CODE

#!/bin/bash

TASK_FILE="tasks.txt"
LOG_FILE="task_scheduler.log"

# Function to display menu


display_menu() {
echo "1. Add a new task"
echo "2. View scheduled tasks"
echo "3. Remove a task"
echo "4. Stop/Resume a task"
echo "5. View execution logs"
echo "6. Exit"
echo -n "Choose an option: "
}

# Function to log messages


log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}

# Function to add a new task


add_task() {
echo -n "Enter the command to schedule: "
read command
echo -n "Enter frequency in seconds: "
read frequency

if [[ ! "$frequency" =~ ^[0-9]+$ ]]; then


echo "Frequency must be a positive number."
return
fi

echo "$frequency|$command|active" >> $TASK_FILE


log_message "Added task: $command with frequency $frequency seconds"
echo "Task added successfully!"
}

# Function to view tasks


view_tasks() {
if [[ ! -s $TASK_FILE ]]; then
echo "No tasks scheduled."
return
fi

echo "Scheduled tasks:"

—4—
nl -s ". " $TASK_FILE | awk -F'|' '{print $1, $2, $3}'
}

# Function to remove a task


remove_task() {
view_tasks
echo -n "Enter the task number to remove: "
read task_number

if sed -n "${task_number}p" $TASK_FILE &>/dev/null; then


sed -i "${task_number}d" $TASK_FILE
log_message "Removed task number $task_number"
echo "Task removed successfully!"
else
echo "Invalid task number!"
fi
}

# Function to stop or resume a task


stop_resume_task() {
view_tasks
echo -n "Enter the task number to stop/resume: "
read task_number

task=$(sed -n "${task_number}p" $TASK_FILE)


if [[ -n "$task" ]]; then
status=$(echo "$task" | awk -F'|' '{print $3}')
if [[ "$status" == "active" ]]; then
sed -i "${task_number}s/active/inactive/" $TASK_FILE
log_message "Paused task number $task_number"
echo "Task paused successfully!"
else
sed -i "${task_number}s/inactive/active/" $TASK_FILE
log_message "Resumed task number $task_number"
echo "Task resumed successfully!"
fi
else
echo "Invalid task number!"
fi
}

# Function to execute tasks


execute_tasks() {
log_message "Task scheduler started."
while true; do
if [[ -s $TASK_FILE ]]; then
while IFS="|" read -r frequency command status; do
if [[ "$status" == "active" ]]; then
(bash -c "$command" &>> $LOG_FILE) &
sleep "$frequency"

—5—
fi
done < $TASK_FILE
fi
sleep 1
done
}

# Function to view logs


view_logs() {
if [[ -s $LOG_FILE ]]; then
cat $LOG_FILE
else
echo "No logs available."
fi
}

# Main Script
if [[ $1 == "--run" ]]; then
execute_tasks
else
while true; do
display_menu
read option
case $option in
1) add_task ;;
2) view_tasks ;;
3) remove_task ;;
4) stop_resume_task ;;
5) view_logs ;;
6) exit ;;
*) echo "Invalid option. Try again." ;;
esac
done
fi

—6—
OUTPUT

Output Scenarios
Scenario 1: Adding a New Task
Command:
./task_scheduler.sh
Menu Output:
1. Add a new task
2. View scheduled tasks
3. Remove a task
4. Stop/Resume a task
5. View execution logs
6. Exit
Choose an option: 1
Input:
Enter the command to schedule: echo "Hello, Advanced World!"
Enter frequency in seconds: 10
Final Output:
Task added successfully!

Scenario 2: Viewing Scheduled Tasks


Command:
./task_scheduler.sh
Menu Output:
1. Add a new task
2. View scheduled tasks
3. Remove a task
4. Stop/Resume a task
5. View execution logs
6. Exit
Choose an option: 2
Final Output:
Scheduled tasks:
1. 10|echo "Hello, Advanced World!"|active

Scenario 3: Pausing a Task


Command:
./task_scheduler.sh
Menu Output:
1. Add a new task
2. View scheduled tasks
3. Remove a task
4. Stop/Resume a task
5. View execution logs

—7—
6. Exit
Choose an option: 4
Input:
Scheduled tasks:
1. 10|echo "Hello, Advanced World!"|active
Enter the task number to stop/resume: 1
Final Output:
Task paused successfully!

Scenario 4: Viewing Execution Logs


Command:
./task_scheduler.sh
Menu Output:
1. Add a new task
2. View scheduled tasks
3. Remove a task
4. Stop/Resume a task
5. View execution logs
6. Exit
Choose an option: 5
Final Output:
Logs:
2025-01-04 10:00:00 - Added task: echo "Hello, Advanced World!" with frequency
10 seconds
2025-01-04 10:05:00 - Paused task number 1

—8—
CONCLUSION

The Linux Task Scheduler Script provides a simple yet effective way to automate
tasks on a Linux system using a Bash script. By offering an intuitive, menu-driven
interface, users can easily schedule commands or scripts to run at specified intervals,
view scheduled tasks, and remove unwanted tasks. The script utilizes basic Bash
features such as loops, conditionals, and file handling to create a lightweight alternative
to more complex scheduling systems like Cron.

Through this project, you have implemented a practical tool for automating repetitive
tasks, making it easier to manage scheduled operations. The script ensures flexibility
and ease of use, enabling both novice and advanced users to schedule and manage tasks
with minimal effort.

In conclusion, this project demonstrates your ability to work with Bash scripting and
Linux task management, while providing a useful tool for automating tasks and
improving productivity on Linux systems.

—9—

You might also like