Monday, September 16, 2024

Scheduler creations in Linux-based OS machines and Windows

Scheduler creations in Linux-based OS machines


Useful link for crontab calculations: https://crontab.guru/

To install crontab in Linux, you can use the following commands:

1. Update the package list: sudo apt update

2. Install cron: sudo apt install cron

3. Start the cron service: sudo systemctl start cron

4. Enable cron to start on system boot: sudo systemctl enable cron 

You can create a cron job by editing the /etc/crontab file. To do this, you can:

Open the crontab configuration file for the current user: crontab -e

Add a line containing a cron expression and the path to a script like below

*/5 * * * * /usr/bin/sh /home/narendra/my_stress_suite.bash > /home/taccuser/narendra/cron_jobs/abc.txt 2>&1

Save (Ctrl+x) and exit the crontab file 

Here are some special characters you can use in the time fields:

Asterisk (*): Specifies that the command should be run for every instance of the field

Hyphen (-): Can be used to indicate a range

Comma (,): Can be used to specify a list of values for a field

Forward slash (/): Can be used to specify step values 

You can also use shorthand extensions to replace the time fields. Some of these options include:

@reboot, @yearly, @annually, @monthly, @weekly, @daily, and @hourly


For shell scripts

*/5 * * * * /usr/bin/sh /home/narendra/my_stress_suite.bash > /home/taccuser/narendra/cron_jobs/abc.txt 2>&1

Explanation:

crontab -l   ==> Displays the contents of the crontab file associated with the current user on the screen

crontab -e   ==> By using this command we can modify the crontab configuration & save (Ctrl+x)

For python scripts

*/5 * * * * /usr/bin/python3 /home/narendra/my_stress_suite.py > /home/taccuser/narendra/cron_jobs/abc.txt 2>&1


Scheduler creations in Windows machines

truncate command in Linux is used to change the size of a file, either by shortening or extending itIt's a useful tool for managing file sizes and optimizing storage space

Syntax: truncate -s [number of bytes] filename_along_with_path

       Ex: truncate -s 0 narendra/cron_jobs/abc.txt

above command will delete the content of the file to make filesize 0 bytes (i.e, the file will present but data will be erased)


==============================================================

RequestIf you find this information useful, please provide your valuable comments.







Wednesday, September 11, 2024

Small realtime usecase Python codes for student to maintain proficiency

# W.A.P for chatbot Response
def chatbot_response(user_input):
user_input = user_input.lower()
if "hello" in user_input or "hi" in user_input:
return "Hello! How can I help you today?"
elif "weather" in user_input:
return "The weather is sunny today!"
else:
return "I'm sorry, I don't understand that."

user_input = input("You: ")
response = chatbot_response(user_input)

print("Chatbot:", response)

==============================================================

"""
Write a python code to validate password, set by the user
Rules:
1. At least 8 characters should be present in the given password
2. At least One Capital letter & lowercase letter should present
3. At least 1 numerical value should present
4. At least 1 special character should present
5. space should not present in the given password
6. password & confirm password should be the same
"""
password = input("Enter New password: ")
Confirm_password = input("Re-enter password: ")
"""Check if password is at least 8 characters long"""
is_valid_length = len(password) >= 8

""" Check if password contains at least one uppercase letter """
has_uppercase = any(char.isupper() for char in password)

""" Check if password contains at least one lowercase letter """
has_lowercase = any(char.islower() for char in password)

""" Check if password contains at least one digit"""
has_digit = any(char.isdigit() for char in password)

"""Check if password contains at least one special character"""
special_characters = "!@#$%^&*()-_+=<>?/|\\{}[]:;\"',."
has_special_char = any(char in special_characters for char in password)

"""Check if password does not contain any spaces"""
has_no_spaces = " " not in password

""" Printing the results """
print(f"Password: {password}")
print(f"Valid Length: {is_valid_length}")
print(f"Contains Uppercase: {has_uppercase}")
print(f"Contains Lowercase: {has_lowercase}")
print(f"Contains Digit: {has_digit}")
print(f"Contains Special Character: {has_special_char}")
print(f"Contains No Spaces: {has_no_spaces}")

""" Final validation result """
if all([is_valid_length, has_uppercase, has_lowercase, has_digit, has_special_char, has_no_spaces]) and password == Confirm_password:
print("Given Password is valid.")
else:
print("Given Password is invalid.")

==============================================================

""" Sentiment analysis
Example text to analyze"""
# text = "I love this product! It is amazing and works perfectly. I am very happy with it."

""" Lists of positive and negative words """
# positive_words = ["love", "amazing", "happy", "good", "great", "fantastic", "excellent", "positive", "enjoy"]
# negative_words = ["hate", "terrible", "bad", "worse", "awful", "horrible", "negative", "sad", "angry"]

"""Convert text to lowercase to ensure case-insensitive comparison """
# text_lower = text.lower()

""" Initialize counters for positive and negative words """
# positive_count = 0
# negative_count = 0

""" Count positive words in the text """
# for word in positive_words:
# positive_count += text_lower.count(word)

""" Count negative words in the text """
# for word in negative_words:
# negative_count += text_lower.count(word)

""" Determine the overall sentiment """
# if positive_count > negative_count:
# sentiment = "Positive"
# elif negative_count > positive_count:
# sentiment = "Negative"
# else:
# sentiment = "Neutral"

""" Print the results """
# print(f"Text: {text}")
# print(f"Positive words count: {positive_count}")
# print(f"Negative words count: {negative_count}")
# print(f"Overall sentiment: {sentiment}")

==============================================================

# Library management system 

# Dictionary to store books and their availability
library = {
"The Great Gatsby": {"author": "F. Scott Fitzgerald", "available": True},
"1984": {"author": "George Orwell", "available": False},
"To Kill a Mockingbird": {"author": "Harper Lee", "available": True}
}

# Check availability of a book
def check_availability(book_title):
if book_title in library:
availability = library[book_title]["available"]
return f"{book_title} is {'available' if availability else 'not available'}."
else:
return f"{book_title} is not in the library."

# Print the availability of a specific book
print(check_availability("1984"))
print(check_availability("The Catcher in the Rye"))

# List all available books
available_books = [book for book, details in library.items() if details["available"]]
print("Available books:", available_books)

==============================================================

# polling system (EVM machine results printing)

# Dictionary to store poll results
poll_results = {
"Option A": 50,
"Option B": 50,
"Option C": 50,
"Option D": 50
}

# Adding new votes
def add_vote(option):
if option in poll_results:
poll_results[option] += 1
else:
poll_results[option] = 1

# Add votes
add_vote("Option A")
add_vote("Option B")
add_vote("Option E")
add_vote("Option B")
# Print poll results
for option, votes in poll_results.items():
print(f"{option}: {votes} votes")

Request: If you find this information useful, please provide your valuable comments